我正在尝试向 Jqgrid 添加新记录。记录已成功添加,但网格在插入记录后不会自行重新加载(刷新)。
<div id="listPager" class="scroll" style="text-align:center;"></div>
<div id="listPsetcols" class="scroll" style="text-align:center;"></div>
<script type="text/javascript">
$(document).ready(function () {
$.jgrid.nav.addtext = "Add";
$.jgrid.nav.edittext = "Edit";
$.jgrid.nav.deltext = "Delete";
$.jgrid.edit.addCaption = "Add Main Category";
$.jgrid.edit.editCaption = "Edit Main Category";
$.jgrid.del.caption = "Delete Main Category";
$.jgrid.del.msg = "Delete selected Main Category?";
$("#list").jqGrid({
url: '@Url.Action("DynamicGridData", "MainCategory")',
datatype: 'json',
mtype: 'GET',
colNames: ['MaincategoryID', 'Category Name'],
colModel: [
{ name: 'MaincategoryID', index: 'MaincategoryID', width: 40, align: 'left', /* key: true,*/editable: true, editrules: { edithidden: false }, hidedlg: true, hidden: true },
{ name: 'MainCategoryName', index: 'MainCategoryName', search: true, width: 300, align: 'left', editable: true, edittype: 'text', editrules: { required: true }, formoptions: { elmsuffix: ' *'}}],
pager: $('#listPager'),
editurl: '@Url.Action("Update", "MainCategory")',
rowNum: 1000,
width: '100%',
height: '100%',
rowList: [1000],
sortname: 'MainCategoryName',
sortorder: "desc",
viewrecords: true,
loadonce: true,
ignoreCase: true,
caption: 'Main Category List',
autowidth: true,
}).navGrid('#listPager',
{
edit: true, add: true, del: true, search: false, refresh: true
}
);
});
</script>
控制器:
public ActionResult DynamicGridData(string sidx, string sord, int page, int rows, bool _search, string searchField, string searchOper, string searchString)
{
rep = new MaincategoryRepository();
var jsonData = rep.DynamicGridData(sidx, sord, page, rows, _search, searchField, searchOper, searchString);
return Json(jsonData, JsonRequestBehavior.AllowGet);
}
public ActionResult Update(lstMainCategory viewModel, FormCollection formCollection)
{
var operation = formCollection["oper"];
if (operation.Equals("add"))// || operation.Equals("edit")
{
rep = new MaincategoryRepository();
lstMainCategory mcat = new lstMainCategory();
mcat.MainCategoryName = viewModel.MainCategoryName;
rep.Create(mcat);
rep.Save();
}
else if (operation.Equals("del"))
{
//delete code
}
return Content("");
}