0

我正在尝试向 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("");
        }
4

1 回答 1

4

您使用loadonce: true选项,因此 jqGrid在第一次从服务器加载数据后更改datatype: "json"为。datatype: "local"稍后重新加载网格,包括添加新行后的重新加载,将在本地进行,而不是从服务器重新加载。

要在表单编辑的添加/编辑操作后强制从服务器重新加载,您可以重置datatype"json"内部onclickSubmitbeforeSubmit回调。您可以在navGrid的参数中设置回调,也可以在$.jgrid.edit. 在最后一种情况下,选项将用于添加和编辑表单:

$.extend($.jgrid.edit, {
    beforeSubmit: function () {
        $(this).jqGrid("setGridParam", {datatype: "json"});
        return [true,"",""];
    }
});
于 2013-06-17T05:23:47.393 回答