asp.net mvc 2 - Collections.Generic.Dictionary<TKey,Tvalue> model binding from Viewmodel to controller -


i have problem : when try post submit view httppost actionresult null value.

this code :

    public class whitelistviewmodel {     public string badge { get; set; }     public ienumerable<string> selezioni { get; set; }     public ienumerable<bool> abilitazioni { get; set; } }   public actionresult whitelist() {       return view( "whitelist", masterpage, new whitelistviewmodel());  }  [httppost] public actionresult whitelistp(ienumerable<whitelistviewmodel> whitelist ) {             bool[] abilitato = new bool[whitelist.single().abilitazioni.count()];             string[] selezione = new string[whitelist.single().selezioni.count()];              ... }        <%@ page title="" language="c#" masterpagefile="~/views/shared/siter.master" inherits="system.web.mvc.viewpage<ienumerable<_21112010.viewmodel.whitelistviewmodel>>" %> <asp:content id="content1" contentplaceholderid="titlecontent" runat="server">     whitelist </asp:content <asp:content id="content2" contentplaceholderid="maincontent" runat="server">     <h2>whitelist</h2>             <table style="width:100%;">      <thead>        </thead>       <tbody >                        <%using ( html.beginform( ) )    {%>               <%  foreach ( var item in model ){%>                     <tr style="width:100%;">                 <td >                 <%: item.badge%>                                 </td>                                 <%foreach ( var abit in item.abilitazioni ){%>                  <td >                                         <%: html.checkboxfor(c=>abit/*, new { onchange = "this.form.submit();" } */ )%>                     <%: html.validationmessagefor(c => abit) %>                 </td>                                        <%  } %>                 <%} %>                  <td style=" width:1px;" >                 <%: html.hiddenfor(model=>item.badge) %>                 <% foreach (var sel in item.selezioni) {%>                 <%: html.hiddenfor(c=>sel) %>                 <%} %>                 </td>                 </tr>                                   <%}%>                                        </tbody>          <tfoot>         </tfoot >         </table>      <input type="submit"  value="salva ed esci" style = "background-color:gray; color:#f6855e; font-weight:bold;  border:1px solid black; height:20px;"  />         <%:html.actionlink( "aggiungi badge", "aggiungibadge")%>                        <% } %>                              </div> </asp:content> 

where doing wrong?

the binding process attempt map ienumerable parameter whitelist of httppost action. however, i'm sure failing because binding process has no information tie submitted fields expected parameter "whitelist".

you have few options:

  1. try using tryupdatemodel() in action;

  2. creating custom modelbinder. allow interogate submitted model , build ienumerable prior being passed action parameter;

  3. you interogate submitted fields using formcollection object within action. little messy , not ideal;

  4. simplify view.

personally, @ modelbinder. if anything, give better insight why model may not binding action parameter.


Comments

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

c# - How to add a new treeview at the selected node? -

java - netbeans "Please wait - classpath scanning in progress..." -