In MVC while embedding the Jquery grid we will have to also perfrom the Crude operations like Insert Update Delete.
For doing that in MVC we need to made actions for it and for each action we need a view .
But in case of JQuery grid we dont need the separate view for doing these operations only one view is sufficient to do it.
First we will made the model that contains the attributes for the entities i want to worked on.
public enum getstatus
{
success, failure
}
public class EmployeeModel
{
public int EmpId { get; set; }
[Required(ErrorMessage = "First Name is required")]
public string FirstName { get; set; }
public string MiddleName { get; set; }
[Required(ErrorMessage = "Last Name is required")]
public string LastName { get; set; }
[Required(ErrorMessage = "PhoneNo is required")]
public string PhoneNo { get; set; }
public string Address { get; set; }
public getstatus Status { get; set; }
public string msg { get; set; }
}
After that we will create the method through which updation is being performed.
EmployeeDetail empdet = new EmployeeDetail();
empdet.EmpFirstName = EmpMod.FirstName;
empdet.EmpMiddleName = EmpMod.MiddleName;
empdet.EmpLastName = EmpMod.LastName;
DataContextObj.EmployeeDetails.InsertOnSubmit(empdet);
DataContextObj.SubmitChanges();
After this we will create action associated with this .
[HttpPost]
public JsonResult Save(EmployeeModel EmpMod)
{
new GridModel().Save(EmpMod);
return Json(true);
}
Then at last we will create the view in which update is being performed.
function Save() {
var EmployeeModel = {
EmpId: $("#EmpId").val(),
FirstName: $("#txtFirstName").val(),
MiddleName: $("#txtMiddleName").val(),
LastName: $("#txtLastName").val()
};
$.ajax({ url: "Home/Save", type: "POST", data: { EmpMod: EmployeeModel } })
.done(function () {
grid.reload();
$("#playerModal").modal("hide");
})
.fail(function () {
alert("Unable to save.");
$("#playerModal").modal("hide");
});
0 Comment(s)