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; }
}
当我调试时,obj
有一个children
属性,但它始终为空......在我的浏览器控制台中,它不为空......