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

4
  • try replacing obj.children = new Object ({ child : childObj }); by obj.children = [{ child : childObj }]; Commented Jun 12, 2013 at 13:26
  • What do you see with Glimpse? What do you see with fiddler? Commented Jun 12, 2013 at 13:26
  • Try sending with JSON.stringify(jsonObj), as my answer in this post Commented Jun 12, 2013 at 13:29
  • 2
    You don't have to use new Object; you can just use {}. Commented Jun 12, 2013 at 13:30

1 Answer 1

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; }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you. The JavaScript was the problem; but your notes about C# are helpful, too.
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 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 api.jquery.com/jquery.ajax

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.