View page:
jQuery("#theGrid").jqGrid({
url: '/Student/Grid',
datatype: "json",
colNames: ['Id', 'Firstname', 'Middlename', 'Lastname', 'Birthdate', 'Birthplace', 'State', 'Nationality', 'Religion', 'Ishandicaped ?', 'Actions'],
colModel: [
{ key: true, name: 'Id', sortable: true, resize: true, align: "center", hidden: true },
{ key: false, name: 'FirstName', index: 'FirstName', editable: true, align: "center", editrules: { required: true } },
{ key: false, name: 'MiddleName', index: 'MiddleName', editable: true, align: "center", editrules: { required: true } },
{ key: false, name: 'LastName', index: 'LastName', editable: true, align: "center", editrules: { required: true } },
{ key: false, name: 'Birthdate', index: 'Birthdate', editable: true, align: "center", editrules: { required: true }, formatter: 'date', editoptions: { dataInit: function (el) { setTimeout(function () { $(el).datepicker(); }, 200); } } },
{ key: false, name: 'Birthplace', index: 'Birthplace', editable: true, align: "center", editrules: { required: true } },
{ key: false, name: 'State', index: 'State', editable: true, align: "center", edittype: "select", editoptions: { value: "GJ:Gujarat;MH:Maharashtra;DL:Delhi;BNG:Banglore;RJ:Rajasthan" }, editrules: { required: true } },
{ key: false, name: 'Nationality', index: 'Nationality', editable: true, align: "center", editrules: { required: true } },
{ key: false, name: 'Religion', index: 'Religion', editable: true, align: "center", editrules: { required: true } },
{
key: false, name: 'IsHandicaped', index: 'IsHandicaped', editable: true, align: "center", edittype: "checkbox", editoptions: { value: "True:False" }
},
{
key: false,
name: 'Actions',
index: 'tax',
width: 80,
align: "center",
formatter: 'actions',
formatoptions: {
keys: true,
delOptions: { url: '/Student/Delete' }
}
}
],
rowNum: 10,
rownumbers: true,
autowidth: true,
rowList: [5, 10, 20],
pager: '#gridPager',
sortname: 'invdate',
viewrecords: true,
sortorder: "acc",
altRows: true,
delicon: 'glyphicon glyphicon-trash',
altclass: 'table table-striped',
jsonReader: {
repeatitems: false
},
editurl: '/Student/Edit',
mtype:"PUT",
height: '100%',
hiddengrid: false
});
jQuery("#theGrid").jqGrid('navGrid', '#gridPager', { "edit": false, "add": false, "del": false, "search": false, view: false });
$("#theGrid").jqGrid('inlineNav', '#gridPager',
{
add: true,
addicon: "ui-icon-plus",
save: true,
saveicon: "ui-icon-disk",
cancel: true,
cancelicon: "ui-icon-cancel",
"refresh": true,
"view": true,
addoptions: {
url: '/Student/Create'
},
addParams: {
addRowParams: {
url: '/Student/Create',
mtype:"POST",
keys: true,
oneditfunc: function (rowid) {
alert("new row with rowid=" + rowid + " are added.");
}
},
afterSubmit: function (response, postdata) {
if (response.responseText == "") {
$(this).jqGrid('setGridParam',
{ datatype: 'json' }).trigger('reloadGrid'); //Reloads the grid after Add
return [true, ''];
} else {
$(this).jqGrid('setGridParam',
{ datatype: 'json' }).trigger('reloadGrid'); //Reloads the grid after Add
return [false, response.responseText];
}
}
}
});
Controller:
public JsonResult Grid(string sidx, string sord, int page, int rows)
{
int pageIndex = Convert.ToInt32(page) - 1;
int pageSize = rows;
IEnumerable<Student> StudentData = db.Students.ToList();
int totalRecords = StudentData.Count();
var totalPages = (int)Math.Ceiling((float)totalRecords / (float)rows);
if (sord.ToUpper() == "DESC")
{
StudentData = StudentData.OrderByDescending(s => s.Id);
StudentData = StudentData.Skip(pageIndex * pageSize).Take(pageSize);
}
else
{
StudentData = StudentData.OrderBy(s => s.Id);
StudentData = StudentData.Skip(pageIndex * pageSize).Take(pageSize);
}
var jsonData = new
{
total = totalPages,
page,
records = totalRecords,
rows = StudentData
};
return Json(jsonData, JsonRequestBehavior.AllowGet);
}
[HttpPost]
[ValidateAntiForgeryToken]
public string Create([Bind(Exclude = "Id")] Student student)
{
string msg;
try
{
if (ModelState.IsValid)
{
db.Students.Add(student);
db.SaveChanges();
msg = "Saved Successfully";
}
else
{
msg = "Validation data not successfull";
}
}
catch (Exception ex)
{
msg = "Error occured:" + ex.Message;
}
return msg;
}
public string Edit(Student student)
{
string msg;
try
{
if (ModelState.IsValid)
{
db.Entry(student).State = EntityState.Modified;
db.SaveChanges();
msg = "Saved Successfully";
}
else
{
msg = "Validation data not successfull";
}
}
catch (Exception ex)
{
msg = "Error occured:" + ex.Message;
}
return msg;
}
//Deletes the product
public string Delete(int Id)
{
Student student = db.Students.Find(Id);
db.Students.Remove(student);
db.SaveChanges();
return "Deleted successfully";
}
When i add new row inline and click on save ,everytime it calls edit method of controller. Can anybody tell me how can i trigger create method of controller to insert new record in database.Thanks in advance.
How to call this create method of controller for insert new row in database?