user6040864
user6040864

Reputation:

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

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

Upvotes: 0

Views: 1388

Answers (1)

John Ephraim Tugado
John Ephraim Tugado

Reputation: 765

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()));

Upvotes: 1

Related Questions