View:
@model MVCCRUD.Models.Employee
@{
ViewBag.Title = "EmployeeList";
}
Add Employee Details
@using (Html.BeginForm("AddOrEdit", "Employee", FormMethod.Post,new {onsubmit="return SubmitForm(this)" }))
{
@Html.HiddenFor(model => model.EmployeeID)
@Html.LabelFor(model => model.Fname, new { @class = "control-label" })
@Html.EditorFor(model => model.Fname, new { htmlAttributes = new { @class = "form-control" } })
@Html.LabelFor(model => model.Lname, new { @class = "control-label" })
@Html.EditorFor(model => model.Lname, new { htmlAttributes = new { @class = "form-control" } })
@Html.LabelFor(model => model.Email, new { @class = "control-label" })
@Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control" } })
}
| Lname | Lname |
|---|
@section scripts{
var dataTable,Popup;
$(document).ready(function () {
dataTable = $("#employeeTable").DataTable({
"ajax": {
"url": "/Employee/GetData",
"type": "POST",
"datatype": "json",
},
"columns": [
{ "data": "Fname" },
{ "data": "Lname" },
{ "data": "Email" },
{
"data": "EmployeeID", "render": function (data) {
}
} ]
});
});
function SubmitForm(form) {
$.ajax({
type: "POST",
url: form.action,
data: $(form).serialize(),
success: function (data) {
if (data.success) {
$.notify(data.message, {
globalPosition: "top center",
className: "success"
})
dataTable.ajax.reload();
}
}
});
return false;
}
function Delete(id) {
if (confirm('Are you sure??'))
{
$.ajax({
type: "POST",
url: '@Url.Action("Delete","Employee")/' + id,
success: function (data)
{
if (data.success)
{
dataTable.ajax.reload();
$.notify(data.message, {
globalPosition: "top center",
className: "success"
})
}
}
});
}
}
}
Controller:
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MVCCRUD.Models;
namespace MVCCRUD.Controllers
{
public class EmployeeController : Controller
{
// GET: Employee
public ActionResult Index()
{
return View();
}
public ActionResult GetData()
{
using (AppdbEntities db = new AppdbEntities())
{
List empList = db.Employees.ToList();
return Json(new { data = empList }, JsonRequestBehavior.AllowGet);
}
}
[HttpGet]
public ActionResult AddOrEdit(int id=0)
{
if (id == 0)
{
return View(new Employee());
}
else
{
using (AppdbEntities db = new AppdbEntities())
{
return View(db.Employees.Where(x => x.EmployeeID == id).FirstOrDefault());
}
}
}
[HttpPost]
public ActionResult AddOrEdit(Employee emp)
{
using (AppdbEntities db = new AppdbEntities())
{
if (emp.EmployeeID == 0)
{
db.Employees.Add(emp);
db.SaveChanges();
return Json(new { success = true, message = "Saved successfully" }, JsonRequestBehavior.AllowGet);
}
else
{
db.Entry(emp).State = EntityState.Modified;
db.SaveChanges();
return Json(new { success = true, message = "Updated successfully" }, JsonRequestBehavior.AllowGet);
}
}
}
[HttpPost]
public ActionResult Delete(int id)
{
using (AppdbEntities db = new AppdbEntities())
{
Employee emp = db.Employees.Where(x => x.EmployeeID == id).FirstOrDefault();
db.Employees.Remove(emp);
db.SaveChanges();
return Json(new { success = true, message = "Deleted successfully" }, JsonRequestBehavior.AllowGet);
}
}
}
}

Laxmidhar SahooPosted Oct 31, 2018, 10:38 PM
- url: '@Url.Action("Edit","Employee")',
toRuchika WablePosted Oct 31, 2018, 1:59 PM
Laxmidhar SahooPosted Oct 31, 2018, 1:57 AM