简体   繁体   English

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

[英]Deleting multiple records in ASP.NET MVC using jqGrid

How can you enable multiple selection in a jqGrid, and also allow users to delete all of the selected rows using an ASP.NET MVC controller? 如何在jqGrid中启用多个选择,还允许用户使用ASP.NET MVC控制器删除所有选定的行?
I have set the delete url property to my /Controller/Delete method, and this works fine if one record is selected. 我已将delete url属性设置为my / Controller / Delete方法,如果选择了一条记录,则此方法可以正常工作。 However, if multiple records are selected, it attempts to send a null value back to the controller where an integer id is required. 但是,如果选择了多个记录,它会尝试将空值发送回需要整数id的控制器。

You can, but you have to write code for it: 你可以,但你必须为它编写代码:

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);
    }

I want to update this for MVC2 and jquery 1.4.2, if you want to pass array parameters to mvc2: 我想为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") }
                });
            }

check http://jquery14.com/day-01/jquery-14 ajax part thx 检查http://jquery14.com/day-01/jquery-14 ajax part thx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM