使用jqGrid删除ASP.NET MVC中的多个记录

时间:2009-11-17 12:02:07

标签: asp.net-mvc jqgrid

如何在jqGrid中启用多项选择,还允许用户使用ASP.NET MVC控制器删除所有选定的行?
我已将delete url属性设置为my / Controller / Delete方法,如果选择了一条记录,则此方法可以正常工作。但是,如果选择了多个记录,它会尝试将空值发送回需要整数id的控制器。

2 个答案:

答案 0 :(得分: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);
    }

答案 1 :(得分:0)

如果你想将数组参数传递给mvc2,我想为MVC2和jquery 1.4.2更新这个:


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