razor - ASP.Net MVC3 Parent Child Model Binding -


i have partial template uses user object model. user has collection of accounts. on partial template have loop follows. _account partial template bound account class

  @foreach (var item in model.accounts)             {                  <tr>                     <td colspan="6">                         <div>                             @html.partial("_account", item)                         </div>                     </td>                 </tr>              } 

in controller method tried

[acceptverbs(httpverbs.post)] public actionresult userdetails(user user, string actiontype) 

but user.accounts collection empty. tried this. still accounts collection empty.

[acceptverbs(httpverbs.post)] public actionresult userdetails(user user,                                [bind(prefix="user.accounts")]                              fixupcollection<account> accounts,                               string actiontype) 

can use default modelbinder implementation achieve or need different?

yep, can use default model binder. need name fields correctly. need loop output this:

... <input type="text" name="user.accounts[0].sometextfield" /> <input type="text" name="user.accounts[0].someothertextfield" /> ... <input type="text" name="user.accounts[1].sometextfield" /> <input type="text" name="user.accounts[1].someothertextfield" /> ... 

if need add/remove accounts, hardcoded indexes little trickier. re-assign names using javascript before postback. it's possible. question gives more detail on model binding:

asp.net mvc: binding complex type select


Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -