2012-06-27 189 views
0

我有我這樣的聯編輯工作不

$(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"); 
     } 

    } 

當我試圖將編輯內容保存到數據庫jQuery代碼。 .i得到這個例外

The parameters dictionary contains a null entry for parameter 'idnr' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult CompOffEdit(Int32, System.DateTime, System.String)' in 'AGS.Hrms.Controllers.CompOffController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. Parameter name: parameters

用戶只能在這裏編輯計算日期和描述......而我從數據庫中選取的ID字段是隱藏的。

有人可以幫我糾正這個問題

回答

1

您應該CompOffEdit行動idnr參數重命名爲id還是應該重命名參數的行編輯到idnr期間發送到服務器的默認id名。你可以使用jqGrid的prmNames: {id: "idnr"}選項。

以同樣的方式,你應該重命名參數的compOffCompOffEdit行動CompOffDatereasonDescription。或者,您可以使用某個類實例,它具有CompOffDateDescription作爲屬性,作爲CompOffEdit操作的參數。如果CompOffDateDescription屬性將使用編輯行中的值進行初始化。

+0

無論你說什麼,再加上我不得不從我的數據庫中發送這個ID參數作爲隱藏字段來查看並使其可編輯,所以當用戶進行內聯編輯時,此值將在我的compoffedit方法的Id字段中。我已更新我的代碼。 –