【发布时间】:2020-12-31 14:29:04
【问题描述】:
我正在尝试通过 AJAX 将对象列表发布到 MVC 控制器,但当它到达控制器时,editedPrices 参数为 0。
我做错了什么?目前我不确定问题出在我发送的数据还是控制器上。
Java 脚本:
$(document).ready(function () {
var newPrices = [
{ "id": "1", "wePrice" : "99", "wdPrice":"79" },
{ "id": "2", "wePrice" :"89", "wdPrice":"59" }
];
editedPrices = JSON.stringify(newPrices);
$.ajax({
contentType: 'application/json; charset=utf-8',
type: "POST",
url: "@Url.Action("EditRates")",
data: {editedPrices : newPrices},
dataType: "json",
success: function (msg) {
alert(msg);
},
error: function (req, status, error) {
alert(error);
}
});
});
c# 对象
public class EditPrices
{
public string id { get; set; }
public string wePrice { get; set; }
public string wdPrice { get; set; }
}
控制器
[HttpPost]
public int EditRates(List<EditPrices> editedPrices )
{
return editedPrices.Count();
}
我尝试过使用[FromBody] 属性,但editedPrices 参数为空(而不是显示计数为0)。
编辑 澄清一下,我可以看到 AJAX 请求中正在发送一些数据。我可以看到控制器正在被调用。所以它自己的 AJAX 请求正在工作。但我要么以“错误”格式发送数据,要么我尝试接收数据的方式有问题。
【问题讨论】:
-
请打开浏览器的开发工具并查看控制台页面。
-
我已经打开了,但它是空白的(没有错误或警告)。
标签: c# json ajax asp.net-mvc asp.net-core-3.1