2015-03-13 78 views
3

請問我可以怎麼做,刪除我的jqgrid中選擇的多個記錄?我嘗試了很多方法,但至今沒有取得任何成功。我會感謝任何能夠幫助我的人。如何使用asp.net mvc在jqgrid中刪除多個寄存器?

jQuery("#grid-table").jqGrid({ 
     //direction: "rtl", 
     url: "/Lojas/GetLojas", 
     datatype: 'json', 

     mtype: 'Get', 
     height: '100%', 
     colNames: [ ' ', 
        'Name', 
        'Description' 
        ], 
     colModel: [ 
      { 
       name: 'myac', index: '', width: 65, fixed: true, sortable: false, resize: false, 
       formatter: 'actions', 
       formatoptions: { 
        keys: true, 
        delOptions: { recreateForm: true, url: '/Lojas/Delete', beforeShowForm: beforeDeleteCallback }, 
        editformbutton: true, editOptions: { recreateForm: true, url: '/Lojas/Edit', closeAfterEdit: true, beforeShowForm: beforeEditCallback, closeOnEscape: true } 
       } 
      }, 
      { key: true, hidden: true, name: 'Id', index: 'Id', sorttype: "int", editable: false }, 
      { key: false, name: 'Name', index: 'Name', editable: true}, 
      { key: false, name: 'Description', index: 'Description', editable: true} 
     ], 

     viewrecords: true, 
     loadonce: true, 
     rowNum: 10, 
     rowList: [5, 10, 15], 
     jsonReader: { 
      root: "rows", 
      page: "page", 
      total: "total", 
      records: "records", 
      repeatitems: false, 
      Id: "0" 
     }, 
     pager: pager_selector, 
     altRows: true, 
     autowidth: true, 
     multiselect: true, 
     multiboxonly: true, 
     sortorder: "desc", 
     multiboxonly: true, 
     caption: "Lojas Cadastradas" 
    }); 

     //navButtons 
    jQuery("#grid-table").jqGrid('navGrid', pager_selector, 
     { 
      edit: true, 
      add: true, 
      del: true, 
      search: true, 
      refresh: true, 
      view: true, 
     }, 
     { 
      url: '/Lojas/Edit', 
      closeOnEscape: true, 
      closeAfterEdit: true, 
      recreateForm: true 
     }, 
     { 
      url: '/Lojas/Create', 
      closeOnEscape: true, 
      closeAfterAdd: true, 
      recreateForm: true 
     }, 
     { 
      url: '/Lojas/Delete', 
      closeOnEscape: true, 
      closeAfterDelete: true, 
      recreateForm: true 
     }, 
     { 
      //search form 
      recreateForm: true, 
      closeOnEscape: true, 
      closeAfterSearch: true, 
      multipleSearch: true 
     }, 
     { 
      //view record form 
      recreateForm: true 
     } 
    ) 

代碼在我的控制器:

public ActionResult Delete(Loja loja) 
    { 
     Loja lojaToDelete = db.Lojas.Find(loja.Id); 
     if (lojaToDelete == null) 
     { 
      return HttpNotFound(); 
     } 
     db.Lojas.Remove(lojaToDelete); 
     db.SaveChanges(); 
     return View(loja); 
    } 

回答

2

我建議您更改刪除功能public ActionResult Delete(Loja loja)的原型

public void Delete(string id) 

在你的代碼的主要問題如下。對應於the documentation jqGrid後id參數到url: '/Lojas/Delete'。您可以使用prmNames重命名id參數的名稱。在這種情況下,您可以使用prmNames: {id: "Id"},但這不是真的需要。

如果需要刪除多個行,然後id字符串將用逗號分隔,你可以使用類似

public void Delete(string id) 
{ 
    var ids = id.Split(','); 
    foreach (lojaId in ids) { 
     Loja lojaToDelete = db.Lojas.Find(lojaId); 
     if (lojaToDelete == null) 
      throw new HttpResponseException(HttpStatusCode.NotFound); 
     db.Lojas.Remove(lojaToDelete); 
    } 
    db.SaveChanges(); 
} 
+0

非常感謝您對您的解決方案,奧列格!我只是改變了你的代碼,將字符串轉換爲整數,以查找數據庫中的記錄,並且此代碼完美工作!謝謝!!! 最終代碼: 'public void Delete(string id) { \t var id = id.Split(','); \t foreach(var lojaId in ID){ \t \t int idLoja = Convert.ToInt32(lojaId); \t \t Loja lojaToDelete = db.Lojas.Find(idLoja); \t \t如果(lojaToDelete == NULL) \t \t \t拋出新HttpResponseException(HttpStatusCode.NotFound); \t \t db.Lojas.Remove(lojaToDelete); \t} \t db.SaveChanges(); }' – David 2015-03-13 15:13:49

+0

@大衛:不客氣!我只是寫了代碼而沒有測試它。我建議您驗證在您的環境中拋出的異常是否正確,或者您應該使用另一個異常。您可以使用'errorTextFormat'回調來自定義刪除錯誤客戶端上顯示的文本。可以在服務器端自定義異常的序列化(請參閱[舊回答](http://stackoverflow.com/a/5501644/315935)中的'HandleJsonExceptionAttribute')。 – Oleg 2015-03-13 15:22:15