我正在尝试将JSON数组发送到ASP MVC Controller。我首先尝试了一个JSON对象,它确实有效,但在我尝试使用数组JSON对象时失败了。正如下面的编码所示,我期待得到 ListForm2是,
ListForm2 [0] .MarketID = 1,......
ListForm2 [1] .MarketID = 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; }
}
答案 0 :(得分:1)
尝试以下方法:
$.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
属性中。