您的位置:首页 > 编程语言 > ASP

ASP.NET 5 - $.ajax post JSON.stringify(para) is null

2015-12-15 11:24 363 查看
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: