JQGrid able to pass ValidateAntiForgeryToken throu

2019-07-22 17:34发布

问题:

This is my first time setting up a jqGrid, so I implemented a basic grid but am having a rather difficult time passing the __RequestVerificationToken to my controller.

$("#RawMatGrid").jqGrid({
        url: "/RawMat/GetRawMats",
        datatype: 'JSON',
        mtype: 'GET',
        colNames: [
            'Item',
            'Product',
            'Description'
        ],
        colModel: [
            { name: 'Item', key: true, index: 'Item', sortable: true, editable: true },
            { name: 'Product', key: true, index: 'Product', sortable: true, editable: true },
            { name: 'Description', key: true, index: 'Description', sortable: true, editable: true }
        ],
        pager: "#paging",
        rowNum: 10,
        rowList: [10, 20, 30, 40, 50],
        width: 780,
        height: 500,
        viewrecords: true,
        caption: 'Raw Mats',
        emptyrecords: 'No records to display',
        autowidth: true,
        multiselect: false,
        jsonReader: {
            root: "rows",
            page: "page",
            total: "total",
            records: "records",
            repeateditems: false,
            Id: "0"
            }
    }).navGrid(
        "#paging", {
            edit: true,
            add: true,
            del: false,
            search: true,
            refresh: true
        },
        { },
        { //Add
            zIndex: 100,
            url: '/RawMat/Create',
            mtype: 'POST',
            // This did not work
            editData: { __RequestVerificationToken: jQuery('input[name=__RequestVerificationToken]').val() },
            processData: "Processing...",
            width: 400,
            closeOnEscape: true,
            closeAfterEdit: true
        },
        {});

After trying to use the editData field and failing horribly, I have come to ask the experts.

I saw an example of someone being able to pass the token via extraparams within their inline, but the navGrid Add does not allow extraparams from what I read on the documentation site. Has anyone had any experience passing it through the main grid's CRUD controls? Any and all help is definitely appreciated!

回答1:

It's definitively wrong to use key: true for more as one column. It break rowids. The id values of rows must have unique value over the HTML page. I recommend you to verify whether jsonReader which you use really corresponds the input data which you use. It looks suspected. If you includes 1-2 rows of input data I could help you to correct jsonReader.

To send __RequestVerificationToken you should define it as function:

editData: { __RequestVerificationToken: function () {
    return $("input[name=__RequestVerificationToken]").val();
}

Alternatively you can use onclickSubmit callback of form editing to extend the data: just replace editData to

onclickSubmit: function (options, postdata, frmoper) {
    return {
        __RequestVerificationToken: $("input[name=__RequestVerificationToken]").val();
    }
}

I included unused parameters of onclickSubmit callback only to show that onclickSubmit allows you to analyse the data which will be sent to the server during editing and to generate the returned data based on the data.