我正在尝试向Jqgrid添加新记录。记录添加成功,但Grid在插入记录后不会重新加载(重新发送)它。
<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("");
}
答案 0 :(得分:4)
您使用loadonce: true
选项,因此在第一次从服务器加载数据后,jqGrid会将datatype: "json"
更改为datatype: "local"
。稍后重新加载网格,包括在添加新行后重新加载,将在本地而不是从服务器重新加载。
要在表单编辑的添加/编辑操作后强制从服务器重新加载,您可以在datatype
或"json"
回调中将onclickSubmit
重置为beforeSubmit
。您可以在navGrid的参数中设置回调,也可以在$.jgrid.edit
中指定回调。在最后一种情况下,选项将用于添加和编辑表单:
$.extend($.jgrid.edit, {
beforeSubmit: function () {
$(this).jqGrid("setGridParam", {datatype: "json"});
return [true,"",""];
}
});