所有相关代码:
//JSON data
var dataType = 'application/json; charset=utf-8';
var data = {
FirstName: 'Andrew',
LastName: 'Lock',
Age: 31
}
console.log('Submitting form...');
$.ajax({
type: 'POST',
url: '/Diary/Save',
dataType: 'json',
contentType: dataType,
data: data,
success: function (result) {
console.log('Data received: ');
console.log(result);
}
});
[HttpPost]
public IActionResult Save([FromBody] Person person)
{
return Json(person);
}
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
}
Save方法中的“person”始终为null。这应该有效...感觉可能像是设置问题?我需要在这里进行更改吗:
services.AddMvc(options =>
{
});
最佳答案
您需要对发送到服务器的数据进行字符串化以将其转换为 JSON,因为这是您指定的内容类型 header :
data: JSON.stringify(data)
关于jquery - 在 ASP.Net Core 中发布 json 时,MVC 模型为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42297519/