asp.netasp.net-mvcjqgridjqgrid-asp.netmvcjqgrid

How to delete multiples registers in jqgrid using asp.net mvc?


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

Solution

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