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

折月煮酒 提交于 2019-12-10 20:46:12

问题


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

回答1:


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


来源:https://stackoverflow.com/questions/29033191/how-to-delete-multiples-registers-in-jqgrid-using-asp-net-mvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!