Ami*_*ari 1 ajax json asp.net-core razor-pages
在 Asp.Net core 中,我有一个 razor 页面,我想将 Ajax 帖子发送到 Post 方法,但我总是得到 null 模型。这是我的简化问题。
public class IndexModel : PageModel
{
public void OnPost([FromBody]A A)
{
if (ModelState.IsValid)
{
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的模型:
[JsonObject(MemberSerialization.OptOut)]
public class A
{
[JsonProperty]
public string Id { get; set; }
[JsonProperty]
public string CityId { get; set; }
[JsonProperty]
public string Infected { get; set; }
[JsonProperty]
public string Susceptible { get; set; }
[JsonProperty]
public string Recovered { get; set; }
[JsonProperty]
public string CityName { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
这是我的 Ajax 请求:
function f(event) {
var token = $("input[name='__RequestVerificationToken']").val();
var c = {};
c["Id"] = "1";
c["CityId"] = "2";
c["Infected"] = "3";
c["Susceptible"] = "4";
c["Recovered"] = "5";
c["CityName"]=""
$.ajax({
url: "./DynamicEpidemic",
type: "post",
contentType: 'application/json; charset=utf-8',
headers:
{
"RequestVerificationToken": token
},
data: { A: JSON.stringify(c)},
success: function () {
alert("OK");
}
});
console.log(JSON.stringify(c));
}
Run Code Online (Sandbox Code Playgroud)
发送的 json 对象是这样的:
{"Id":"1","CityId":"2","Infected":"3","Susceptible":"4","Recovered":"5","CityName":""}
但我的模型始终为 null.ModelState 错误是
解析值时遇到意外字符:A.路径 '',第 0 行,位置 0。
| 归档时间: |
|
| 查看次数: |
2340 次 |
| 最近记录: |