3

jqgrid で選択した複数のレコードを削除する方法を教えてください。私はいくつかの方法を試しましたが、今のところ成功していません。私を助けてくれる人に感謝します。

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

私のコントローラーのコード:

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

1 に答える 1

2

Delete 関数のプロトタイプを次のように変更することをお勧めしますpublic ActionResult Delete(Loja loja)

public void Delete(string id)

コードの主な問題は次のとおりです。ドキュメントのjqGrid postidパラメータに対応しますurl: '/Lojas/Delete'idを使用してパラメータの名前を変更できますprmNames。を使用できますがprmNames: {id: "Id"}、実際には必須ではありません。

複数の行を削除する必要がある場合、id文字列はカンマで区切られ、次のようなものを使用できます

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();
}
于 2015-03-13T14:26:22.373 に答える