2009-11-17 50 views
3

如何在jqGrid中啓用多個選擇,還允許用戶使用ASP.NET MVC控制器刪除所有選定的行?
我已經將delete url屬性設置爲我的/ Controller/Delete方法,並且如果選擇了一條記錄,這可以正常工作。但是,如果選擇了多個記錄,它會嘗試將空值發送回需要整數ID的控制器。使用jqGrid刪除ASP.NET MVC中的多個記錄

回答

2

可以,但你必須爲它編寫代碼:

deleteSelected: function(grid) { 
    if (!grid.jqGrid) { 
     if (console) { 
      console.error("'grid' argument must be a jqGrid"); 
     } 
     return; 
    } 
    var ids = grid.getGridParam('selarrrow'); 
    var count = ids.length; 
    if (count == 0) return; 
    if (confirm("Delete these " + count + " records?")) { 
     $.post("DeleteMultiple", 
      { ids: ids }, 
      function() { grid.trigger("reloadGrid") }, 
      "json"); 
    } 
} 

    [HttpPost] 
    public ActionResult DeleteMultiple(IEnumerable<Guid> ids) 
    { 
     if (!Request.IsAjaxRequest()) 
     { 
      // we only support this via AJAX for now. 
      throw new InvalidOperationException(); 
     } 
     if (!ids.Any()) 
     { 
      // JsonError is an internal class which works with our Ajax error handling 
      return JsonError(null, "Cannot delete, because no records selected."); 
     } 
     var trans = Repository.StartTransaction(); 
     foreach (var id in ids) 
     { 
      Repository.Delete(id); 
     } 
     trans.Commit(); 
     return Json(true); 
    } 
0

我想更新這對MVC2和jQuery 1.4.2,如果你想通過數組參數MVC2:

 

var ids = $("#grid").getGridParam('selarrrow'); 
var postData = { values: ids }; 
if (confirm("Delete these " + count + " records?")) { 
       $.ajax({ 
        type: "POST", 
        traditional: true, 
        url: "GridDBDemoDataDeleteMultiple", 
        data: postData, 
        dataType: "json", 
        success: function() { $("#grid").trigger("reloadGrid") } 
       }); 
      } 

檢查http://jquery14.com/day-01/jquery-14 ajax部分 thx