3

Could you please help me how I could do to delete multiple records selected in my jqgrid? I've tried a number of ways, but so far not got any success. I will be grateful to anyone who can help me.

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

Code in my controller:

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);
    }
David
  • 269
  • 1
  • 3
  • 10

1 Answers1

2

I recommend you to change prototype of Delete function public ActionResult Delete(Loja loja) to

public void Delete(string id)

The main problem in your code is the following. Corresponds to the documentation jqGrid post id parameter to url: '/Lojas/Delete'. You can rename the name of id parameter using prmNames. In the case you can use prmNames: {id: "Id"}, but it's not really required.

If multiple rows needed be deleted then id string will be comma separated and you can use something like

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();
}
Oleg
  • 220,925
  • 34
  • 403
  • 798
  • Thank you very much for your solution, Oleg! I just changed your code to convert the string to integer to find the record in database and this code worked perfectly! Thank you!!! Final code: `public void Delete(string id) { var ids = id.Split(','); foreach (var lojaId in ids) { int idLoja = Convert.ToInt32(lojaId); Loja lojaToDelete = db.Lojas.Find(idLoja); if (lojaToDelete == null) throw new HttpResponseException(HttpStatusCode.NotFound); db.Lojas.Remove(lojaToDelete); } db.SaveChanges(); }` – David Mar 13 '15 at 15:13
  • @David: You are welcome! I just wrote the code without testing it. I recommend you to verify whether the thrown exception is correct in your environment or you should use another one. You can use `errorTextFormat` callback to customize the text displayed on the client side of delete error. One can customize on the server side the serialization of exceptions (see `HandleJsonExceptionAttribute` in [the old answer](http://stackoverflow.com/a/5501644/315935)). – Oleg Mar 13 '15 at 15:22