jqgrid中的内联编辑无效

时间:2012-06-27 12:23:02

标签: jquery asp.net-mvc jqgrid

我有像这样的jQuery代码

$(document).ready(function () {

         var lastsel;

    $('#jqgProducts').jqGrid({
             //url from wich data should be requested
             url: '@Url.Action("CompOff")',
         //type of data
         datatype: 'json',
         //url access method type

         mtype: 'POST',
         //columns names

         ondblClickRow: function (id) {
             if (id && id !== lastsel) {
                 jQuery('#list').restoreRow(lastsel);
                 jQuery('#list').editRow(id, true);
                 lastsel = id;
             }
             $('#jqgProducts').editRow(id, true,null, null);
         },

         editurl: '@Url.Action("CompOffEdit")',
         colNames: ['IdNr', 'CompOffDate', 'Description', 'ExpiryDate', 'IsApproved', 'AppliedOn'],
         //columns model
         colModel: [
                    { name: 'Id', index: 'Id', align: 'center', editable: true, hidden: true},
                        { name: 'CompOffDate', index: 'CompOffDate', align: 'center', formatter: 'date', formatoptions: { newformat: 'd/m/Y' }, editable: true },
                          { name: 'Description', index: 'Description', align: 'center', editable: true, editoptions: { maxlength: 200} },
                        { name: 'ExpiryDate', index: 'ExpiryDate', align: 'center', formatter: 'date', formatoptions: { newformat: 'd/m/Y' }, editable: false },
                        { name: 'IsApproved', index: 'IsApproved', align: 'center', editable: false },

                        { name: 'AppliedOn', index: 'AppliedOn', align: 'center', formatter: 'date', formatoptions: { newformat: 'd/m/Y' }, editable: false }

                      ],
         //pager for grid
         pager: $('#jqgpProducts'),
         //number of rows per page
         rowNum: 10,
         //initial sorting column
         sortname: 'CompOffDate',
         //initial sorting direction
         sortorder: 'asc',
         //we want to display total records count
         viewrecords: true,

         caption: 'Comp Off Details',
         //grid height
         height: '100%',

         jsonReader: {
             root: "rows",
             page: "page",
             total: "total",
             records: "records",
             repeatitems: false,
             cell: "cell",
             id: "id",
             userdata: "userdata"
         }
     });

});

$(document).ready(function () { var lastsel; $('#jqgProducts').jqGrid({ //url from wich data should be requested url: '@Url.Action("CompOff")', //type of data datatype: 'json', //url access method type mtype: 'POST', //columns names ondblClickRow: function (id) { if (id && id !== lastsel) { jQuery('#list').restoreRow(lastsel); jQuery('#list').editRow(id, true); lastsel = id; } $('#jqgProducts').editRow(id, true,null, null); }, editurl: '@Url.Action("CompOffEdit")', colNames: ['IdNr', 'CompOffDate', 'Description', 'ExpiryDate', 'IsApproved', 'AppliedOn'], //columns model colModel: [ { name: 'Id', index: 'Id', align: 'center', editable: true, hidden: true}, { name: 'CompOffDate', index: 'CompOffDate', align: 'center', formatter: 'date', formatoptions: { newformat: 'd/m/Y' }, editable: true }, { name: 'Description', index: 'Description', align: 'center', editable: true, editoptions: { maxlength: 200} }, { name: 'ExpiryDate', index: 'ExpiryDate', align: 'center', formatter: 'date', formatoptions: { newformat: 'd/m/Y' }, editable: false }, { name: 'IsApproved', index: 'IsApproved', align: 'center', editable: false }, { name: 'AppliedOn', index: 'AppliedOn', align: 'center', formatter: 'date', formatoptions: { newformat: 'd/m/Y' }, editable: false } ], //pager for grid pager: $('#jqgpProducts'), //number of rows per page rowNum: 10, //initial sorting column sortname: 'CompOffDate', //initial sorting direction sortorder: 'asc', //we want to display total records count viewrecords: true, caption: 'Comp Off Details', //grid height height: '100%', jsonReader: { root: "rows", page: "page", total: "total", records: "records", repeatitems: false, cell: "cell", id: "id", userdata: "userdata" } });

我的控制器就像这样

   public ActionResult CompOffEdit(int Id,DateTime CompOffDate, string Description)
    {
        RegisterCompOff r = db.RegisterCompOffs.Where(l=>l.Id==Id).SingleOrDefault();
        if (!(r == null))
        {
            r.CompOffDate = CompOffDate;
            r.Description = Description;
            db.Entry(r).State = EntityState.Modified;
            db.SaveChanges();
            return Content("true");
        }
        else
        {
            return Content("false");
        }

    }

当我试图将编辑保存到db..i时会出现此异常

public ActionResult CompOffEdit(int Id,DateTime CompOffDate, string Description) { RegisterCompOff r = db.RegisterCompOffs.Where(l=>l.Id==Id).SingleOrDefault(); if (!(r == null)) { r.CompOffDate = CompOffDate; r.Description = Description; db.Entry(r).State = EntityState.Modified; db.SaveChanges(); return Content("true"); } else { return Content("false"); } }

用户只能在此编辑评论日期和说明...我隐藏了从数据库中挑选的我的id字段。

有人可以帮助我纠正这个问题

1 个答案:

答案 0 :(得分:1)

您应该将idnr操作的CompOffEdit参数重命名为id,或者您应该在行编辑期间重命名发送到服务器的参数的默认id名称idnr。您可以使用jqGrid的prmNames: {id: "idnr"}选项。

以同样的方式,您应该将compOff操作的CompOffEdit参数重命名为CompOffDate,将reason重命名为Description。或者,您可以使用某个类实例(CompOffDateDescription作为属性)作为CompOffEdit操作的参数。如果CompOffDateDescription属性将使用编辑行中的值进行初始化。