簡體   English   中英

free-jqgrid 內聯編輯拋出狀態 400 錯誤

[英]free-jqgrid Inline edit throws status 400 error

該項目是一個 .NET 6 C# MVC 應用程序。 Free-JQgrid 版本是 4.15.5,相信問題在於網格創建 ajax 帖子的方式。 這是網格的代碼。

var mainColModel = [
        {
            name: "act", template: "actions", label: "Edit / Delete", width: 80, align: 'center', sortable: false, formatter: 'actions', fixed: false, resizable: true,
            formatoptions: {
                keys: true, editformbutton: false, delOptions: {
                    afterSubmit: function (jqXhr) {
                        $('#notifications').html(jqXhr.responseText);
                    }
                },
                isDisplayButtons: function (options, rowData) {
                    if (rowData.newGroup) {
                        return { edit: { display: true }, del: { display: true }, formedit: { display: false } };
                    } else {
                        return { edit: { display: false }, del: { display: false } };
                    }
                }
            }
        },
        { name: 'id', key: true, sortable: false, hidden: true },
        { name: 'newGroup', sortable: false, hidden: true },
        {
            name: "name", label: "Item", autoResizable: true, width: 500, editoptions: { maxlength: "25" }, editable: true, editrules: { required: true }, edittype: 'text',
            searchoptions: { sopt: ["eq", "ne", 'bw', 'bn', 'ew', 'en', 'cn', 'nc'] },
        },
    ];
    $grid.jqGrid({
        autoencode: true,
        url: groupListURL,
        editurl: groupEditURL,
        datatype: 'json',
        //ajaxEditOptions: {
        //    type: "POST",
        //    contentType: 'application/json; charset=utf-8',
        //    headers: { headerName: headerToken },
        //    xhrFields: { withCredentials: true },
        //    cache: false
        //},
        //serializeEditData: function (postData) {
        //    return {
        //        functionName: "editGroup",
        //        functionParams: JSON.stringify(postData)
        //    }
        //},
        mtype: 'GET',
        jsonReader: { id: "id" },
        caption: "LookUp Group Maintenance",
        datatype: 'json',
        loadonce: true,
        colModel: mainColModel,
        emptyrecords: "No records found. Add a new Group.",
        height: '100%',
        shrinkToFit: true,
        rownumbers: false,
        autoresizeOnLoad: false,
        guiStyle: "jQueryUI",
        iconSet: "fontAwesomeSVG",
        rowNum: 20,
        autoResizing: { compact: true },
        cmTemplate: { editable: false, autoResizable: true },
        inlineEditing: {
            keys: true,
            aftersavefunc: function (rowid, jqXhr, postdata, options) {
                $('#notifications').html(jqXhr.responseText);
                $(this).trigger("reloadGrid", [{ current: true, fromServer: true }]);
            }
        },
        rowList: [20, 40, 60, "10000:All"],
        viewrecords: true,
        pager: true,
        sortname: "LookUpGroup",
        sortorder: "asc",
        altRows: true,
        altclass: 'jqGridAltRow',
        ondblClickRow: function (id) { },
        forceClientSorting: true,
            });
            $subGrid.jqGrid('inlineNav', inLineOp);
            $subGrid.jqGrid('navGrid', { add: false, edit: false, del: false, search: false, refresh: false }, {}, {}, {});
        }
    });
    $grid.jqGrid('inlineNav', inLineOp);
    $grid.jqGrid('navGrid', { add: false, edit: false, del: false, search: false, refresh: false }, {}, {}, {});

基於 SO 上的搜索,我嘗試了注釋掉的部分,但它仍然拋出錯誤。 代碼中其他地方有幾個地方成功執行了 ajax post。 這是其中之一。

$.ajax({
            url: '@Url.Action("PrintReports", "Reports")',
            type: "POST",
            headers: { '@tokenSet.HeaderName' : '@tokenSet.RequestToken' },
            data: $('form').serialize(),
            dataType: 'json',
            xhrFields: { withCredentials: true },
            cache: false,
            complete: function (data) {
                $("#main").html(data.responseText);
            }
        });

我懷疑這是 xhrFields 和標題的選項。 我試圖在注釋代碼的網格中添加這些但沒有成功。 在升級到 .NET 6 並實施身份驗證之前,它可以正常工作,沒有任何錯誤。 為了完整起見,這里是 controller 中的操作。

    [HttpPost]
    public IActionResult EditGroup(LookUpGroupViewModel model, IFormCollection formCollection)
    {
...
        return PartialView("_Notification", messages);
}

如何解決網格內顯示的錯誤 400?

我能夠通過添加 beforeSubmit 選項來解決問題。 在 function 中執行 ajax 允許添加認證信息。 盡管已修復,但出現了另一個錯誤,即 405 錯誤。 我能夠確定這是一個防偽令牌問題。 現在我已經從應用程序中刪除了這個功能,但將嘗試在 AJAX 調用中實現它。

這對我來說似乎有點老套,希望 Oleg 能夠為至少發布的 ajaxOptions 提供額外的屬性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM