我正在尝试将JSON数据从ajax调用传递给我的操作方法 -
Ajax call -
$.ajax({
type: "POST",
url: '/ProjectList/getProjects',
dataType: "json",
contentType: "application/json; charset=utf-8",
data: JSON.stringify(modelData),
success: function (data) {
alert("Testing");
}
});
模型 -
EbitModel.cs
public class EbitModel
{
public string ProgramName { get; set; }
public string ProjectName { get; set; }
public string SubprojectName { get; set; }
}
控制器的动作方法 -
ProjectListController.cs
[HttpPost]
public ActionResult getProjects(List<EbitModel> modelData)
{
return null;
}
但是我在这个动作方法中的modelData中得到了null。
这里有什么我想念的吗?
编辑 -
ModelData中的JSON数据为 -
[{"ProgramName":"South East","ProjectName":"XYZ","SubprojectName":"LMZ"},{"ProgramName":"South East","ProjectName":"ACB","SubprojectName":"LMZa"}]
答案 0 :(得分:1)
你必须将modelData
和Json传递给控制器。
请尝试以下代码。
var jsonString = [{"ProgramName":"South East","ProjectName":"XYZ","SubprojectName":"LMZ"},{"ProgramName":"South East","ProjectName":"ACB","SubprojectName":"LMZa"}];
var jsonData = JSON.parse('{"modelData":'+jsonString +'}')
$.ajax({
type: "POST",
url: '/ProjectList/getProjects',
dataType: "json",
contentType: "application/json; charset=utf-8",
data: jsonData,
success: function (data) {
alert("Testing");
}
});
答案 1 :(得分:0)
创建一个变量来保存模型并将此变量传递给ajax数据
var modelHolder = { "Value": @Model.Value }
$.ajax({
type: "POST",
url: '/ProjectList/getProjects',
dataType: "json",
contentType: "application/json; charset=utf-8",
data: modelHolder,
success: function (data) {
alert("Testing");
}
});
答案 2 :(得分:0)
我找到了一些解决方法 -
var modelData = { "modelData": dt };
$.ajax({
type: "POST",
url: '/ProjectList/getProjectsFromEbit',
dataType: "html",
contentType: "application/json; charset=utf-8",
data: JSON.stringify(modelData),
success: function (data) {
},
error: function (data) {
}
});
在控制器中 - 我接受了该字符串并对其进行了反序列化
public ActionResult getProjectsFromEbit(string modelData)
{
var systemOutputList = new List<EbitModel>();
systemOutputList = JsonConvert.DeserializeAnonymousType(modelData, systemOutputList);
return PartialView("_getProjects",systemOutputList);
}