我有一个 MVC Controller ,它以某种方式向它传递 Null
Controller :
[HttpPost]
public ActionResult UpdateAllNodes(IEnumerable<ReportGroup> reportGroups)
{
// this is custom return..
return JsonCamel(null);
}
问题:reportGroups 为null
javascript ajax 帖子
$.post(updateAllNodeUri, JSON.stringify({ reportGroups: homogeneous.data() }));
Chrome 开发工具表单数据:
{"reportGroups":[
{"Id":1,"ReportGroupName":"Standard Reports","ReportGroupNameResID":null,"SortOrder":1,"items":[],"index":0},
{"Id":2,"ReportGroupName":"Custom Reports","ReportGroupNameResID":null,"SortOrder":2,"items":[],"index":1},
{"Id":3,"ReportGroupName":"Retail Reports","ReportGroupNameResID":null,"SortOrder":3,"items":[],"index":2},
{"Id":4,"ReportGroupName":"Admin Reports","ReportGroupNameResID":null,"SortOrder":5,"items":[],"index":3},
{"Id":5,"ReportGroupName":"QA Reports","ReportGroupNameResID":null,"SortOrder":4,"items":[],"index":4},
{"ReportGroupName":"Node","index":5}
]}:
所以我在 Controller 中有reportGroups以及JSON作为reportGroups:因此我不明白为什么它是空的。
这里还有 ReportGroup 的 poco 类
public class ReportGroup : BaseEntity
{
public override int Id { get; set; }
public string ReportGroupName { get; set; }
public int? ReportGroupNameResID { get; set; }
public int SortOrder { get; set; }
}
Kendo 数据调用,然后我可以输出到控制台并查看数据
console.log(homogeneous.data());
// ### Send the Data to the server
var updateAllNodeUri = "/Report/UpdateAllNodes";
最佳答案
根据您从 Chrome 开发工具获取的 JSON 数据,您实际上是使用此模型发送 JSON:
JSON:
{"reportGroups":[
{"Id":1,"ReportGroupName":"Standard Reports","ReportGroupNameResID":null,"SortOrder":1,"items":[],"index":0},
{"Id":2,"ReportGroupName":"Custom Reports","ReportGroupNameResID":null,"SortOrder":2,"items":[],"index":1},
{"Id":3,"ReportGroupName":"Retail Reports","ReportGroupNameResID":null,"SortOrder":3,"items":[],"index":2},
{"Id":4,"ReportGroupName":"Admin Reports","ReportGroupNameResID":null,"SortOrder":5,"items":[],"index":3},
{"Id":5,"ReportGroupName":"QA Reports","ReportGroupNameResID":null,"SortOrder":4,"items":[],"index":4},
{"ReportGroupName":"Node","index":5}
]}:
型号:
public class ReportGroup
{
public int Id { get; set; }
public string ReportGroupName { get; set; }
public object ReportGroupNameResID { get; set; }
public int SortOrder { get; set; }
public List<object> items { get; set; }
public int index { get; set; }
}
public class RootObject
{
public List<ReportGroup> reportGroups { get; set; }
}
RootObject 类是 Controller 接收到的类,它不是预期的参数。
尝试将 JSON 转换为以下格式:
[
{"Id":1,"ReportGroupName":"Standard Reports","ReportGroupNameResID":null,"SortOrder":1,"items":[],"index":0},
{"Id":2,"ReportGroupName":"Custom Reports","ReportGroupNameResID":null,"SortOrder":2,"items":[],"index":1},
{"Id":3,"ReportGroupName":"Retail Reports","ReportGroupNameResID":null,"SortOrder":3,"items":[],"index":2},
{"Id":4,"ReportGroupName":"Admin Reports","ReportGroupNameResID":null,"SortOrder":5,"items":[],"index":3},
{"Id":5,"ReportGroupName":"QA Reports","ReportGroupNameResID":null,"SortOrder":4,"items":[],"index":4},
{"ReportGroupName":"Node","index":5}
]
Javascript:
$.post(updateAllNodeUri, JSON.stringify(homogeneous.data()));
关于javascript - MVC Controller 从具有数据的 JavaScript 对象传入 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36073913/