如何将 JSON 数组发送到 ASP.NET MVC 控制器
How to send JSON Array to an ASP.NET MVC Controller
我正在尝试将 JSON 数组发送到 ASP MVC 控制器。我首先尝试了一个 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; }
}
尝试以下操作:
$.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
属性中。
我正在尝试将 JSON 数组发送到 ASP MVC 控制器。我首先尝试了一个 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; }
}
尝试以下操作:
$.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
属性中。