ASP.NET Core MVC/WebAPi 模型绑定

时间:2023-03-10 01:49:49
ASP.NET Core MVC/WebAPi 模型绑定
  public class Person
{
public string Name { get; set; }
public string Address { get; set; }
public int Age { get; set; }
}
 $("#btnJson").on("click", function () {
var datajson = { Name: "Jeffcky", Age: 24, Address: "湖南省" };
console.log(datajson);
$.ajax({
url: "../api/WebAPi/PostPerson",
contentType: "application/x-www-form-urlencoded;charset=utf-8",
dataType: "json",
type: "post",
data: datajson,
success: function (data) {
console.log(data);
}
});
});
     [HttpPost]
public IActionResult PostPerson([FromBody]Person p)
{
return Json(p);
}