asp.net mvc - nested JSON object not mapping to server side Object -
i'm trying send json string controller using ajax call. seems working fine except nested object within json string not mapping nested object on controller side. here's example of code.
class myclass1{ string prop1; string prop2; string prop3; myclass2 prop4; } class myclass2{ string prop5; string prop6; string prop7; }
and here's how i'm trying map.
$("#btnsave").click(function () { $.ajax({ type: 'post', url: '@url.action("member", "application")', data: { applicantmodel:getappdata(), buttontext: $("#btnsave").val(), isdeleted: $("#hdndeleted").val() }, datatype: "json", success: function (data) { }, error: function (xhr, ajaxoptions, error) { } }); }) getappdata = function () { var fields = { "prop1": "a", "prop2": "b", "prop3": "c", "prop4":{ "prop5": "d", "prop6": "e", "prop7": "f" } } return fields; }
and controller method...
<httppost()> function member(applicantmodel applicantmodels, buttontext string, isdeleted string) actionresult //do stuff data return view(applicantmodel) end function
when break inside controller, object myclass1 mapped correctly prop4 data not mapped. have tried several ideas other threads, have tried json.stringify() json before sending in ajax call. out of ideas. love help!
thanks in advance!
you need make view model properties public
set
, get
accessors, defaultmodel binder can set values of these properties form data posted.
public class myclass1 { public string prop1 { get; set; } public string prop2 { get; set; } public string prop3 { get; set; } public myclass2 prop4 { get; set; } } public class myclass2 { public string prop5 { get; set; } public string prop6 { get; set; } public string prop7{ get; set; } }
with change, code should work fine.
Comments
Post a Comment