List count empty when passing from view to model in ASP.Net MVC -


before begin, have looked @ question:

asp.net mvc model binding fails <list> items in editor template

i have model looks so:

public class partnerlistmodel {     public list<partnersmodel> partners { get; set; }      public partnerlistmodel()     {         partners = new list<partnersmodel>();     } } 

and partnersmodel looks such:

public class partnersmodel {     public int id { get; set; }     public string name { get; set; }     public bool isactive { get; set; } } 

i'm passing partnerslistmodel view, looks this:

using (html.beginform("update", "partners", formmethod.post))             {                 @html.editorfor(m => m.partners)                 <input type="submit" value="submit changes"/>             } 

and finally, editor template looks this:

@model adminwebsite.models.partners.partnersmodel <div>     <span>         @html.displayfor(m => m.name)     </span>     <span>         @html.editorfor(m => m.isactive)     </span>     @html.hiddenfor(m => m.id)     @html.hiddenfor(m => m.name) </div> 

my controller's action so, , code manage hit action:

public actionresult update(partnerlistmodel partners) 

why list inside model has count of 0? can't find reason why example differs accepted answer on stack overflow. there missing explain why data values passed not being added list?

using chrome's developer tools i've been able confirm have list looks similar following:

  • partners[0].isactive = true
  • partners[0].isactive = false
  • partners[0].name = "hi"
  • partners[0].id = 1
  • partners[1].isactive = false etc.

any ideas? in advance.

you're passing list<partnersmodel> action method, action-method expects partnerlistmodel.

you should change action-method this:

public actionresult update(list<partnersmodel> partners) 

the reason default modelbinder doesn't nest graph. if want access both items, need include both parameters:

public actionresult update(partnerlistmodel partnerlistmodel, list<partnersmodel> partners){  } 

if want nested model, you'd have implement own modelbinder.

the following question has similar problem: binding editable list of children


Comments

Popular posts from this blog

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

keyboard - C++ GetAsyncKeyState alternative -

android - java.net.UnknownHostException(Unable to resolve host “URL”: No address associated with hostname) -