[英]free-jqgrid Inline edit throws status 400 error
The project is a .NET 6 C# MVC application.该项目是一个 .NET 6 C# MVC 应用程序。 Free-JQgrid version is 4.15.5 and believe the issue is with the way the grid has the ajax post created.
Free-JQgrid 版本是 4.15.5,相信问题在于网格创建 ajax 帖子的方式。 Here is the code for the grid.
这是网格的代码。
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 }, {}, {}, {});
Based upon searches here on SO, I have tried the commented out sections and it still threw the error.基于 SO 上的搜索,我尝试了注释掉的部分,但它仍然抛出错误。 There are a couple places elsewhere in the code that execute an ajax post successfully.
代码中其他地方有几个地方成功执行了 ajax post。 This is one of them.
这是其中之一。
$.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);
}
});
I suspect it is the options of xhrFields and headers.我怀疑这是 xhrFields 和标题的选项。 I attempted to add these in the grid in the commented code but was unsuccessful.
我试图在注释代码的网格中添加这些但没有成功。 Prior to upgrading to .NET 6 and implementing the authentication it worked without any errors.
在升级到 .NET 6 并实施身份验证之前,它可以正常工作,没有任何错误。 For completeness here is the action in the controller.
为了完整起见,这里是 controller 中的操作。
[HttpPost]
public IActionResult EditGroup(LookUpGroupViewModel model, IFormCollection formCollection)
{
...
return PartialView("_Notification", messages);
}
How do I resolve the Error 400 displayed inside the grid?如何解决网格内显示的错误 400?
I was able to resolve the issue by adding the beforeSubmit option.我能够通过添加 beforeSubmit 选项来解决问题。 Executing the ajax within that function allowed the addition of the authentication information.
在 function 中执行 ajax 允许添加认证信息。 Although that was fixed another error arose and that was 405 error.
尽管已修复,但出现了另一个错误,即 405 错误。 I was able to determine that was an antiforgery token problem.
我能够确定这是一个防伪令牌问题。 For now I have removed this feature from the application but will be trying to implement it in the AJAX call.
现在我已经从应用程序中删除了这个功能,但将尝试在 AJAX 调用中实现它。
This does seem a little hacky to me and hope that Oleg adds the ability to provide additional properties to the ajaxOptions for at least the posting.这对我来说似乎有点老套,希望 Oleg 能够为至少发布的 ajaxOptions 提供额外的属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.