如何将 JSON 数组发送到 ASP.NET MVC 控制器
本文关键字:ASP NET MVC 控制器 JSON 数组 | 更新日期: 2023-09-27 18:36:03
我正在尝试将JSON数组发送到ASP MVC控制器。 我首先尝试了一个 JSON 对象,它确实有效,但在我尝试数组 JSON 对象时失败了。 如下编码所示,我希望得到ListForm2 是,
ListForm2[0]。市场 ID = 1, ...
ListForm2[1]。市场 ID = 2, ...
但是,我得到了ListForm2=null。 我的编码出了什么问题?
function ProcessSaveView(area, bChecked, bChart, saveName) {
var jsondata = [{ MarketID: 1, ForecastPointTypeID: 5, ForecastPointID: 21, CustomTimeZoneID: "ET-Prevailing", IsChart: true },
{ MarketID: 2, ForecastPointTypeID: 5, ForecastPointID: 51, CustomTimeZoneID: "ET-Prevailing", IsChart: true }];
$.ajax({
type: "POST",
url: "./charts/SaveViewToDatabase",
dataType: "json",
traditional: true,
data: jsondata,
success: function (result) {
if (result.success) {
alert("Save View successful!");
} else {
alert("Duplicate View already exist. Not save!");
}
},
error: function () {
alert("No market, load type & region is selected!");
}
});
}
[HttpPost]
public ActionResult SaveViewToDatabase(testJsonObject[] ListForm2)
{
return Json(new { success = true });
}
public class testJsonObject
{
public int ForecastPointID { get; set; }
public int MarketID { get; set; }
public int ForecastPointTypeID { get; set; }
public string CustomTimeZoneID { get; set; }
public bool IsChart { get; set; }
}
尝试以下操作:
$.ajax({
type: "POST",
url: "./charts/SaveViewToDatabase",
dataType: "json",
contentType: "application/json",
data: JSON.stringify(jsondata),
success: function (result) {
if (result.success) {
alert("Save View successful!");
} else {
alert("Duplicate View already exist. Not save!");
}
},
error: function () {
alert("No market, load type & region is selected!");
}
});
请参阅添加contentType
并JSON.stringify
到data
属性中。