0

I have an MVC controller that somehow is passing in Null to it

Controller:

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

PROBLEM : reportGroups is null

javascript ajax post

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

Chrome Dev Tools Form Data:

{"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.

Also here here the poco class for the ReportGroup

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

console.log(homogeneous.data());

// ###  Send the Data to the server 

var updateAllNodeUri = "/Report/UpdateAllNodes";
2
  • BTW, this IS using Kendo UI , thus you see it ADDS some stuff : items and index . These are NOT in my poco class, but should that matter? Commented Mar 18, 2016 at 0:13
  • 1
    If you stringify the data, you also need to set the contentType: 'json' option - try using the $.ajax() method to include this option. Commented Mar 18, 2016 at 0:18

1 Answer 1

1

Based on the JSON data you get from Chrome Dev Tools, you are actually sending the JSON with this model:

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.

Try transforming the JSON to this format:

[
    {"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()));
Sign up to request clarification or add additional context in comments.

2 Comments

Well if you even mock that up with a simple class and use fiddler, you will see that removing "reportGroups" is not even going to work. ( I realize that it might not look correct from a pure JSON format with brackets [ ] , but that is just not it
The structure you have would work with an without the {"reportGroups": ... with Chrome Extension POSTMAN - just his (OP) issue was with the header needed to specify the content-type of json ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.