【问题标题】:mvc controller is passing in null from javascript object that has datamvc 控制器从具有数据的 javascript 对象传入 null
【发布时间】:2016-03-18 00:10:49
【问题描述】:

我有一个 MVC 控制器,它以某种方式将 Null 传递给它

控制器:

[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}
]}:

所以我在控制器中有 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; }
}

剑道数据调用,然后我可以输出到控制台并查看数据

console.log(homogeneous.data());

// ###  Send the Data to the server 

var updateAllNodeUri = "/Report/UpdateAllNodes";

【问题讨论】:

  • 顺便说一句,这是使用 Kendo UI,因此您会看到它添加了一些东西:项目和索引。这些不在我的 poco 课上,但这有关系吗?
  • 如果你对数据进行字符串化,你还需要设置contentType: 'json'选项——尝试使用$.ajax()方法来包含这个选项。

标签: javascript c# json asp.net-mvc asp.net-mvc-routing


【解决方案1】:

根据您从 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 类是控制器接收到的,它不是预期的参数。

尝试将 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()));

【讨论】:

  • 好吧,如果你甚至用一个简单的类来模拟它并使用 fiddler,你会发现删除“reportGroups”甚至都行不通。 (我意识到从带有括号 [ ] 的纯 JSON 格式来看,它可能看起来不正确,但事实并非如此
  • 如果没有 {"reportGroups": ... with Chrome Extension POSTMAN,您所拥有的结构就可以使用 - 只是他的(OP)问题在于指定 json 的内容类型所需的标头...
猜你喜欢
  • 2015-09-13
  • 2014-11-21
  • 2014-10-18
  • 1970-01-01
  • 2012-08-03
  • 1970-01-01
  • 2018-10-07
  • 2015-12-28
  • 1970-01-01
相关资源
最近更新 更多