javascript - Sending complex object in ajax to MVC -
the value of list<order>
returns null
in controller action method while sending complex object. can identify issue? need pass array of objects indexes?
javascript
function oncustomerclick() { //var orders = []; //orders.push({ 'orderid': '1', 'orderby': 'saroj' }); var complexobject = { firstname: 'saroj', lastname: 'k', //orders : orders orders: [{ orderid: 1, orderby: 'saroj' }, { orderid: 2, orderby: 'kumar' }] }; var obj = { customer: complexobject }; var data2send = json.stringify(obj); $.ajax({ type: "post", url: 'home/testcustomer1', data: data2send, contenttype: "application/json; charset=utf-8", datatype: "json", success: function (arg) { //call successfull }, error: function (xhr) { //error occurred } }); };
mvc
public actionresult testcustomer1(customer customer) { return json(customer); }
c#
public class customer { public string firstname { get; set; } public string lastname { get; set; } list<order> orders { get; set; } } public class order { public int orderid { get; set; } public string orderby { get; set; } }
you need use public properties model binding. orders
has no access modifier, private.
public class customer { public string firstname { get; set; } public string lastname { get; set; } public list<order> orders { get; set; } // <---- }
other that, looks fine.
Comments
Post a Comment