jquery - Issues binding complex json model in MVC3 controller -


i have been struggling post work in project. bascially, passing list of install objects ( install object contains list of facility objects) controller. have yet binding work successfully. have been able count of list items reflect number of items passed controller, none of properties being set in individual objects.

here code performs post:

$("#openfacilityresults").button().click(function () {   var installholder = {     installlist: []   }   var foundinstall = false   $('.fullexport').each(function () {     //var install = { installrecnum: 0, facilitylist: [facility, facility] }     //var facility = { id: 0, product: 0 }     if ($(this).prop('checked')) {       var substr = $(this).parent().find('input:hidden').val().split('|');       var fac = {         id: substr[1],         product: substr[2]       }       foundinstall = false       $.each(installholder.installlist, function (index, value) {         if (value.installrecnum == substr[0]) {           foundinstall = true           installholder.installlist[index].facilitylist.push(fac);         }       });       if (foundinstall == false) {         var = {           installrecnum: substr[0],           facilitylist: []         }         i.facilitylist.push(fac)         installholder.installlist.push(i)       }     }   });   console.log(json.stringify(installholder));   var dataholder = json.stringify(installholder)   $.ajax({     url: '@url.action("viewfacilitydetails", "chi")',     type: 'post',     datatype: 'json',     data: dataholder,     contenttype: 'application/json',     success: function (data) {       // result , magic     }   }); }); 

here json looks like:

{"installlist":[{"installrecnum":"140","facilitylist":[{"id":"0","product":"1"}]},{"installrecnum":"138","facilitylist":[{"id":"0","product":"1"}]}]} 

here controller action:

function viewfacilitydetails(byval installlist list(of installinputmodel)) actionresult     return json(installlist) end function 

and objects attempting bind:

<serializable()> _ public class installinputmodel     public installrecnum integer     public facilitylist list(of facilityinputmodel) end class <serializable()> _ public class facilityinputmodel     public id integer     public product integer end class 

any appreciated - in advance!

update

here working code accomplish this:

custom model binder:

public class jsonbinder     implements imodelbinder      public function bindmodel(controllercontext system.web.mvc.controllercontext, bindingcontext system.web.mvc.modelbindingcontext) object implements system.web.mvc.imodelbinder.bindmodel         if controllercontext nothing             throw new argumentnullexception         end if         if bindingcontext nothing             throw new argumentnullexception         end if          dim request = controllercontext.httpcontext.request          request.inputstream.seek(0, io.seekorigin.begin)         dim jsonstring = new streamreader(request.inputstream).readtoend          if not jsonstring nothing              dim deserializer new system.web.script.serialization.javascriptserializer             dim result = deserializer.deserialize(of installinputmodelholder)(jsonstring)              return result         else             return nothing         end if      end function end class 

*gloabl.asax.vb addition *

modelbinders.binders.add(gettype(installinputmodelholder), new jsonbinder()) 

i'll change json can pass array , container class won't needed deserialize json.

create modelbinder bind jsonstring list(of installinputmodel) , register in global.asax

off top of head (c#):

 public class jsonbinder : imodelbinder {     public object bindmodel(controllercontext controllercontext, modelbindingcontext bindingcontext)     {         //return base.bindmodel(controllercontext, bindingcontext);         if (controllercontext == null)         {             throw new argumentnullexception("controllercontext");         }          if (bindingcontext == null)         {             throw new argumentnullexception("bindingcontext");         }           // json data that's been posted         var request = controllercontext.httpcontext.request;          request.inputstream.seek(0, seekorigin.begin);         var jsonstring = new streamreader(request.inputstream).readtoend();           if (jsonstring != null)         {             var serializer = new javascriptserializer();             var result = serializer.deserialize(jsonstring, typeof(list<installinputmodel>));             return result;          }         else         {             return null;         }       } } 

in application_start of global.asax, add:

modelbinders.binders.add(typeof(list<installinputmodel>), new jsonbinder()); 

Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -