我正在尝试使用 ajax 请求将数据发布到 Asp.Net Core 2.1 Razor 页面。
$.ajax({
type: "POST",
url: "/MyPage",
data: { "Key1": Val1},
beforeSend: function (xhr) {
xhr.setRequestHeader("XSRF-TOKEN",
$('input:hidden[name="__RequestVerificationToken"]').val());
},
contentType: false,
processData: false,
success: function (response) {
if (response == "Success")
alert("Successfully saved.");
else {
alert(response);
}
},
error: function (e) {
alert(e.responseText);
}
});
在这里一切正常。
现在,我想传递大约 50 个文本框的值,因此与其使用data: { "Key1": Val1},
有没有其他方法可以绑定[BindProperty] class
?
我的页面模型外观
[BindProperty]
public InputModel Input { get; set; }
有关 .cshtml 的更多信息
https://jsfiddle.net/4sb8vqda/1/
可以使用序列化方法:
$.ajax({
type: "POST",
url: "/MyPage",
headers: { "RequestVerificationToken": $('input[name="__RequestVerificationToken"]').val() },
data: $('#theFormId').serialize(),
success: function (response) {
if (response == "Success")
alert("Successfully saved.");
else {
alert(response);
}
},
error: function (e) {
alert(e.responseText);
}
});
您设置了错误的标头值。此外,不要将contenttype
设置为false
。这会导致contenttype
设置为text/plain
,而不是application/x-www-form-urlencoded; charset=UTF-8
您需要传输表单值