简体   繁体   English

mvc控制器从具有数据的javascript对象传入null

[英]mvc controller is passing in null from javascript object that has data

I have an MVC controller that somehow is passing in Null to it 我有一个以某种方式将Null传递给它的MVC控制器

Controller: 控制器:

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

PROBLEM : reportGroups is null 问题reportGroups

javascript ajax post javascript ajax发布

$.post(updateAllNodeUri, JSON.stringify({ reportGroups: homogeneous.data() }));

Chrome Dev Tools Form 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}
]}:

So I have the reportGroups in controller along with the JSON as reportGroups: thus I'm lost on why it is null. 所以我将reportGroups与JSON一起放在控制器中,作为reportGroups:因此,我迷失了为什么它为null。

Also here here the poco class for the ReportGroup 这里也是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 data calls , then I can output to console and see the data as well Kendo数据调用,然后我可以输出到控制台并查看数据

console.log(homogeneous.data());

// ###  Send the Data to the server 

var updateAllNodeUri = "/Report/UpdateAllNodes";

Based on the JSON data you get from Chrome Dev Tools, you are actually sending the JSON with this model: 根据您从Chrome开发工具获得的JSON数据,您实际上是使用以下模型发送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}
]}:

Model: 模型:

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; }
}

The RootObject class is the one being received by your controller which is not the expected parameter. RootObject类是控制器收到的类,不是期望的参数。

Try transforming the JSON to this format: 尝试将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: Javascript:

$.post(updateAllNodeUri, JSON.stringify(homogeneous.data()));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Javascript将null传递给MVC控制器 - Javascript passing null to MVC Controller 数据从Javascript传递到MVC控制器 - Data passing from Javascript to MVC controller 从Javascript MVC控制器传递数据 - Passing data from Javascript MVC controller 将数据收集对象从Jquery ajax方法传递到显示空对象的MVC控制器 - Passing data collection object from Jquery ajax Method to MVC Controller showing null object Javascript 对象作为空对象传递给控制器​​“未捕获的 ReferenceError:数据未定义” - Javascript object is passing to the controller as null object "Uncaught ReferenceError: data is not defined" ASP.NET MVC通过Java序列化到Controller的AJAX通过BeginCollectionItem List的对象传递null - ASP.NET MVC Passing a BeginCollectionItem List's Object through AJAX with Javascript Serialize to Controller is passing null 将数据从 javaScript 传递到 MVC 控制器视图 ajax - passing data from javaScript to MVC controller view ajax 使用多个参数将数据从javascript传递到MVC控制器 - Passing data from javascript to MVC Controller with multiple parameters 将Javascript对象数组传递给MVC控制器 - Passing Javascript Object array to MVC Controller 在mvc控制器中接收为空的Javascript对象 - Javascript object received as null in mvc controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM