Removing multiple records in ASP.NET MVC using jqGrid

How can you enable multiple selection in jqGrid, and also allow users to delete all selected rows using ASP.NET MVC controller?
I set the url delete property for the / Controller / Delete method, and it works fine if one record is selected. However, if multiple records are selected, it tries to send a null value back to the controller, where an integer identifier is required.

+3
source share
2 answers

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);
    }
+2
source

I want to update this for MVC2 and jquery 1.4.2 if you want to pass array parameters to 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 part

0

Source: https://habr.com/ru/post/1723094/


All Articles