0

JqG​​rid 编辑功能在我的网格中运行良好,但删除功能始终将记录 ID 传递为 0。

@model Shop.Models.ShopModels.lstMainCategory
<table id="list" class="scroll" cellpadding="0" cellspacing="0"></table>
    <div id="pager" class="scroll" style="text-align:center;"></div>                                                            

    <script type="text/javascript">

        var myGrid = $('#list');
        $(document).ready(function () {

            $.jgrid.nav.addtext = "Add";
            $.jgrid.nav.edittext = "Edit";
            $.jgrid.nav.deltext = "Delete";
            $.jgrid.edit.addCaption = "Add Main Category";
            $.jgrid.edit.closeAfterAdd = true;
            $.jgrid.edit.closeAfterEdit = true;
            $.jgrid.edit.editCaption = "Edit Main Category";
            $.jgrid.del.caption = "Delete Main Category";
            $.jgrid.del.msg = "Delete selected Main Category?";

            $.extend($.jgrid.edit, {
                beforeSubmit: function () {
                    $(this).jqGrid("setGridParam", { datatype: "json" });
                    return [true, "", ""];
                }
            });

            $("#list").jqGrid({
                url: '/MainCategory/DynamicGridData/',
                datatype: 'json',
                mtype: 'GET',

                colNames: ['Edit', 'Main Category'],
                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: 700, align: 'left', editable: true, edittype: 'text', editrules: { required: true }, formoptions: { elmsuffix: ' *'} }],
                pager: jQuery('#pager'),
                editurl: '@Url.Action("Update", "MainCategory")',
                rowNum: 10,
                width: '100%',
                height: '100%',
                rowList: [5, 10, 20, 50],
                viewrecords: true,
                loadonce: true,
                ignoreCase: true,
                caption: 'Main Categories'

            }).navGrid('#pager', { edit: true, add: true, del: true, search: true, searchtext: "Search" });

            $("#list").jqGrid('filterToolbar', { stringResult: true, searchOnEnter: false, defaultSearch: 'cn' });

            $("#list").setGridParam({ data: results.rows, localReader: reader }).trigger('reloadGrid');


    </script>

控制器:

  public ActionResult Update(lstMainCategory viewModel, FormCollection formCollection)
        {
            var operation = formCollection["oper"];
            lstMainCategory mcat = new lstMainCategory();
            rep = new MaincategoryRepository();
            if (operation.Equals("add"))
            {              

                mcat.MainCategoryName = viewModel.MainCategoryName;
                rep.Create(mcat);
                rep.Save();
            }
            else if (operation.Equals("edit"))
            {
                mcat.MaincategoryID = viewModel.MaincategoryID;
                mcat.MainCategoryName = viewModel.MainCategoryName;
                rep.Edit(mcat);
                rep.Save();
            }
            else if (operation.Equals("del"))
            {

                rep.Delete(viewModel.MaincategoryID);//here MaincategoryID is always 0 so it's not working
                rep.Save();
            }

            return Content("true");
        }

编辑

MainCategoryID  MainCategory
16           aaa
17           bbb

在此处输入图像描述

存储库:

 public object DynamicGridData(string sidx, string sord, int page, int rows, bool _search, string searchField, string searchOper, string searchString)
        {

            dc = new ShopDataContext(DBConnection.Connection);

            int pageIndex = Convert.ToInt32(page) - 1;
            int pageSize = rows;
            int totalRecords = dc.tblMainCategories.Count();
            int totalPages = (int)Math.Ceiling((float)totalRecords / (float)pageSize);

            List<MainCategory> lst = (from tbm in dc.tblMainCategories
                                select new MainCategory
                                {
                                    MaincategoryID=tbm.MainCategoryID,
                                    MainCategoryName=tbm.MainCategory,
                                    DeleteID=tbm.MainCategoryID
                                }).ToList<MainCategory>().OrderBy(x=>x.MainCategoryName).ToList();

            List<MainCategory> Flst;



            if (_search && searchField == "MainCategory")
            {
                Flst = lst.Where(r => r.MainCategoryName.Contains(searchString)).OrderBy(x => x.MainCategoryName).ToList().ToList();
            }
            else
                Flst = lst;


            var GridData = new
            {
                total = totalPages,
                page = page,
                records = totalRecords,
                rows = (
                    from mcat in Flst
                    select new
                    {
                        i = mcat.MaincategoryID,
                        cell = new string[] { mcat.MaincategoryID.ToString(), mcat.MainCategoryName.ToString(),mcat.DeleteID.ToString() }
                    }).ToArray()
            };

            return GridData;
        }
4

1 回答 1

2

首先,您应该修复您使用的服务器响应的格式。它没有i属性,但它有id属性。线

i = mcat.MaincategoryID,

必须固定到

id = mcat.MaincategoryID,

您可以替换代码

rows = (
    from mcat in Flst
    select new
    {
        i = mcat.MaincategoryID,
        cell = new string[] { mcat.MaincategoryID.ToString(), mcat.MainCategoryName.ToString(),mcat.DeleteID.ToString() }
    }).ToArray()

rows = (
    from mcat in Flst
    select new[] {
        mcat.MaincategoryID.ToString(),
        mcat.MainCategoryName,
        mcat.DeleteID.ToString()
    }).ToArray()

没有重复的相同信息(发送mcat.MaincategoryID两次)并从您发送的每个项目中删除"id:""cell:"字符串。要在客户端读取此类更紧凑的数据,您应该jsonReader: { cell: "" }向 jqGrid 添加选项。如果您使用最新版本的 jqGrid,则该选项是可选的。

如果您希望MaincategoryID发送包含列的内容,则应使用列editrules: { edithidden: true }中的选项MaincategoryID(请参阅答案)。或者,您可以添加prmNames: {id: "MaincategoryID"}. 在这种情况下,jqGrid 会将 rowid 作为“MaincategoryID”而不是默认值发送"id"

如果您不需要显示MaincategoryID列,则可以将其从colModel. 在这种情况下,您可以将服务器返回的数据顺序更改为

rows = (
    from mcat in Flst
    select new[] {
        mcat.MainCategoryName,
        mcat.MaincategoryID.ToString(),
        mcat.DeleteID.ToString()
    }).ToArray()

jqGrid 中的更改将是

colNames: ['Edit', 'Main Category'],
colModel: [
    { name: 'MainCategoryName', width: 700, editable: true,
        editrules: { required: true }, formoptions: { elmsuffix: ' *'} }
],
pager: "#pager",
gridview: true,
jsonReader: {cell: "", id: 1},
prmNames: {id: "MaincategoryID"}

jqGrid 将id属性分配给每一行(每个<tr>)。使用sonReader: {id: 1}一个通知 jqGrid 从行数据的第二项 ( mcat.MaincategoryID.ToString()) 中获取 rowid。其他更改pager: "#pager"gridview: true我之前在答案中写给您的相同。<table id="list" ...您应该从and中删除额外的垃圾<div id="paber" ...(请参阅我的旧答案)

于 2013-06-17T11:48:53.087 回答