JavaScript 代码:
var para = {};
para.id = $("#ad-text-id").val();
para.title = $("#ad-text-title").val().trim();
para.link = $("#ad-text-link").val().trim();
$.ajax({url: '/ajax/AdText/SaveAdText',data: JSON.stringify(para),type: 'post',dataType: 'json',contentType: 'application/json; charset=utf-8',cache: false,success: function (data) {},error: function (xhr) {}
});
Action 代码:
[HttpPost]
public async Task<JsonResult> SaveAdText(int id, string title, string link)
{//id, title, link para is null...
}
使用 ASP.NET 5,Action 获取到的参数都为 null,但使用之前的 ASP.NET MVC 5 是可以的,需要更改下代码:
public class Model
{public int Id { get; set; }public string Title { get; set; }public string Link { get; set; }
}[HttpPost]
public async Task<JsonResult> SaveAdText([FromBody]Model model)
{//model.Id, model.Title, model.Link is not null...
}
ASP.NET 5 POST Model,需要使用 FromBody。
参考:$.ajax post JSON.stringify(para) is null