我错过了一些东西,我需要一些帮助。我没办法。一切看起来都正确,但我在控制器动作方法中得到了模型的所有空值。
任何输入都将不胜感激。谢谢!
我的控制器上有以下操作方法......
[HttpPost]
public JsonResult RunReport(ReportSubmitModel model)
{
}
我有以下模特类...
public class ReportSubmitModel
{
public string[] RequiredParameters { get; set; }
public string[] Companies { get; set; }
public string[] AssetTypes { get; set; }
public string[] ExpenseStatuses { get; set; }
public string StartDate { get; set; }
public string EndDate { get; set; }
public string ReportGuid { get; set; }
}
这是我的Json通过调用JSON.stringify()并将其输出到控制台的样子。
[{"ReportGuid":"1011e87e-074b-4a40-bea6-ed271ab1e7ca"},{"Companies":["434b5d92-c41e-40b7-8490-11ed0047b232","966b8291-ff4c-4254-a475-cdc1277d014d","775ce236-a17f-41f0-b254-415163217181","129de43b-6fb1-475e-a6d2-28143750c973","fec4475a-e992-4560-b9b1-efd486ad10ca"]},{"AssetTypes":["192a3095-b33f-474f-af02-557ad5cb69f6","192a3095-b33f-474f-af02-557ad5cb69f6","34e3b858-a682-4936-8e43-c663a4df2cd2"]},{"RequiredParameters":["C","AT","FS","CS","SCH"]},{"StartDate":"1/1/2012"},{"EndDate":"1/1/2013"},{"ExpenseStatuses":["1","2"]}]
这是我的ajax调用的样子......
.ajax({
type: "POST",
data: JSON.stringify(postInfo),
dataType: "json",
contentType: "application/json; charset=utf-8",
url: "/Home/RunReport",
success: loadReport});
更新:postInfo是一个我将值推到的数组。
例如,postInfo.push({ReportGuid:“guidvalue”});
答案 0 :(得分:4)
因为所有那些额外的花括号。我不知道你是如何构建javascript对象的,但它不应该是{
。有了这些,每个项目都被视为对象,因此您实际上将一个集合对象传递给您的控制器方法。当你的对象被字符串化时,它应该是这样的:
{"ReportGuid": "1011e87e-074b-4a40-bea6-ed271ab1e7ca" ,
"Companies":["434b5d92-c41e-40b7-8490-11ed0047b232",
"966b8291-ff4c-4254-a475-cdc1277d014d",
"775ce236-a17f-41f0-b254-415163217181"]
注意我在{
字段之前和之后删除了Companies
。实际上你可以像js那样构建它,就像这样(我删除了其他部分以简化示例):
var postInfo =
{"ReportGuid": "1011e87e-074b-4a40-bea6-ed271ab1e7ca" ,
"Companies":["434b5d92-c41e-40b7-8490-11ed0047b232",
"966b8291-ff4c-4254-a475-cdc1277d014d"]};