c# - Posted models (via jQuery) come back with all properties set to default -


i fetch of data models mvc app , operate on them on client side. when i'm done whatever user doing post model/array of models server.

in specific scenario, i'm posting array 2 models in app. can see values of model in http...but when they're deserialized , passed controller come out "empty" -- is, properties set default values, collection has 2 elements in it.

any ideas cause of this? sounds mvc isn't able deserialize http typed objects, can't see why "looks" ok in http. there process missing?

if it's important have tried "receiving" data formal parameters of type ilist , mymodel[]...doesn't seem help.

hard me come specific code, here goes -- perhaps help

// client side  var models = new array(); for(var in selectedmodels) {     var item = selectedmodels[i];      if(typeof(item) != 'function') {         models.push(item);     } }  $.ajax({     type: "post",     data: { mymodels: models, name: "" },     datatype: "json",     url: "/json/my-controller/create-models",     success: function (data, successcode, httprequest) {         // stuff     } })  // server side  [httppost, actionname("create-models")] public actionresult dosomething(mymodel[] mymodels) {     if(mymodels == null || mymodels.length < 1)     {         throw new invalidoperationexception("you must provide @ least 1 model add collection.");     }      // someother stuff...     return json(somenewmodel); } 

see blog post. need use json2.js library stringify data prior posting. stringify method allows javascript data structures serialised transfer on http. need json model binder reconstruct object server side.

your js code :

var models = new array(); for(var in selectedmodels) {     var item = selectedmodels[i];      if(typeof(item) != 'function') {         models.push(item);     } }  var postdata = json.stringify(models);  $.ajax({     type: "post",     data: postdata ,     datatype: "json",     url: "/json/my-controller/create-models",     success: function (data, successcode, httprequest) {         // stuff     } }) 

Comments

Popular posts from this blog

c++ - How do I get a multi line tooltip in MFC -

asp.net - In javascript how to find the height and width -

c# - DataTable to EnumerableRowCollection -