javascript - MVC Controller 从具有数据的 JavaScript 对象传入 null

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

我有一个 MVC Controller ,它以某种方式向它传递 Null

Controller :

[HttpPost]
public ActionResult UpdateAllNodes(IEnumerable<ReportGroup> reportGroups)
{
    // this is custom return..
    return JsonCamel(null);
}

问题:reportGroupsnull

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:因此我不明白为什么它是空的。

这里还有 ReportGrouppoco

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/

相关文章:

javascript三元运算符计算字符串中的字符数

javascript - blueimp fileupload - 上传前检查图像的宽度和高度

c# - 使用LINQ获取具有属性=另一个对象属性的对象

C++ RapidJSON 清除写入的字符串

安卓 JSONException : JSONArray must start with [ - and it does

javascript - JSON 解析错误 - JSON 中位置 1 处出现意外标记 o

javascript - 如何在函数中输入fetch,出现Uncaught SyntaxError : Invalid or unexpected token on web browser

c# - 如何根据我的数据库行以编程方式在 C# 中填充 WPF 网格?

c# - 设置变量 - 构造函数/获取/设置 C#

android - 如何解析和显示 json 数组中的 url 内容?