3

jQuery:

$('#test').click(function () {

    var obj = new Object();
    var childObj = new Object();

    childObj.name = 'dominic';
    childObj.age = 22;

    obj.children = new Object ({ child : childObj });

    console.log(obj);

    $.ajax({
        url: '@Url.Action("Test")',
        type: 'POST',
        data: obj,
        dataType: 'json',
        success: function (msg) {
            //console.log(msg.status);
        }
    });

});

C# (MVC 4):

public JsonResult Test(testObj obj)
{
    foreach (childObj child in obj.children)
    {
        Debug.Print(child.name);
        Debug.Print(child.age);
    }

    return Json(null);
}

public class testObj
{
    public List<childObj> children { get; set; }
}

public class childObj
{
    public string name { get; set; }
    public string age { get; set; }
}

When I debug, obj has a children property, but it is always null... In my browser console, it is not null...

user1477388
  • 20,790
  • 32
  • 144
  • 264
  • try replacing `obj.children = new Object ({ child : childObj });` by `obj.children = [{ child : childObj }];` – Claudio Redi Jun 12 '13 at 13:26
  • What do you see with [Glimpse](http://getglimpse.com/)? What do you see with [fiddler](http://fiddler2.com/)? – Oded Jun 12 '13 at 13:26
  • Try sending with JSON.stringify(jsonObj), as my answer in [this post](http://stackoverflow.com/questions/16954125/json-posted-array-has-null-values/16954189?noredirect=1#comment24483646_16954189) – Paritosh Jun 12 '13 at 13:29
  • 2
    You don't have to use `new Object`; you can just use `{}`. – Dan Jun 12 '13 at 13:30

1 Answers1

10

First I would recommend you sending complex objects from the client to the server as JSON:

$.ajax({
    url: '@Url.Action("Test")',
    type: 'POST',
    contentType: 'application/json',
    data: JSON.stringify(obj),
    dataType: 'json',
    success: function (msg) {
        //console.log(msg.status);
    }
});

Things to notice:

  1. contentType: 'application/json'
  2. data: JSON.stringify(obj)

Also your children property on the client must be an array, not a property:

var obj = {};
var childObj = {};

childObj.name = 'dominic';
childObj.age = 22;

obj.children = [];
obj.children.push(childObj);

or simply:

var obj = {
    children: [
        { name: 'dominic', age: 22 }
    ]
};

Another remark: On the server your Age property is defined as string whereas on the client you are passing it as integer (age: 22) which is inconsistent. In addition to that you don't need to put all your C# properties in lowercase. That's just horrible. The JavaScriptSerializer is intelligent enough to respect the standard C# naming convention:

public class TestObj
{
    public List<ChildObj> Children { get; set; }
}

public class ChildObj
{
    public string Name { get; set; }
    public string Age { get; set; }
}
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Thank you. The JavaScript was the problem; but your notes about C# are helpful, too. – user1477388 Jun 12 '13 at 14:53
  • As you said above about the Content Type in the Ajaxify piece, `contentType: 'application/json'` . I saw in many places `charset = UTF-8`. Why do we add this in the content type ? – Pankaj Jun 13 '13 at 12:10
  • @Pankaj content-type is only needed if you are receiving data back and you want to specify the type of data that is. The default is `'application/x-www-form-urlencoded; charset=UTF-8'` as per http://api.jquery.com/jquery.ajax/ – user1477388 Oct 08 '15 at 14:20