c# - Returning viewmodel from view gives an ampty object in the controller -


i'm working on small project. intention maintain presences of couple of users on specific dates. dates first chosen user.

therefor use view model. provide possible data check box in table show user.

in view walk every day in given view modeland let view generate tag , editor object (html.editorfor ()). surrounded form creation (@ html.beginform ()). rendering view not problem.

the problem when try return view model. viewmodel object receive object null - objects in themselves. has been created default constructor.

controller:

[httpget]     public actionresult opldagenlt()     {         var lt = _gebruikerrepository.geeftraject(_gebruikerrepository.trajectid);         list<datummodel> data = new list<datummodel>();         foreach (opleidingdag opldag in lt.geefopleidingdagenkleinerdanofgelijkaanvandaag())             data.add(new datummodel(opldag));          list<toekomstmodel> toekomst = new list<toekomstmodel>();         foreach (opleidingdag opldag in lt.geefopleidingdagengroterdanvandaag())             toekomst.add(new toekomstmodel(opldag));          datamodel datamod = new datamodel();         datamod.datums = data;         datamod.toekomst = toekomst;          if (lt.controleeraantalopleidingdagen())         {              _gebruikerrepository.gekozendagen = gekozendagen(datamod) list<opleidingdag>;             return redirecttoaction("index", "aanwezigheid");         }         return view(datamod);     }      [httppost]     public actionresult opldagenlt(datamodel model)     {         if (!modelstate.isvalid)         {             return view(model);         }          _gebruikerrepository.gekozendagen = gekozendagen(model) list<opleidingdag>;          return redirecttoaction("index", "aanwezigheid");     }      public icollection<opleidingdag> gekozendagen(datamodel datamod)     {         list<opleidingdag> dagen = new list<opleidingdag>();             foreach (datummodel datum in datamod.datums)             {                 dagen.add(datum.dag);             }         return dagen;     } 

view:

@using(@html.beginform( "opldagenlt", "leertraject", formmethod.post)) { <div>     <table>         <tr>             <th colspan="2">reeds begonnen dagen</th>         </tr>         @foreach (var item in @model.datums)         {             <tr>                 <td>@html.displayfor(modelitem => item.dag.dag.day)/@html.displayfor(modelitem => item.dag.dag.month)/@html.displayfor(modelitem => item.dag.dag.year)</td>                 <td>@html.editorfor(modelitem => item.checked)</td>             </tr>         }     </table> </div> <div id="toekomst">     <table>         <tr>             <th>dagen in de toekomst</th>         </tr>         @foreach (var item in @model.toekomst)         {             <tr>                 <td>@html.displayfor(modelitem => item.dag.dag.day)/@html.displayfor(modelitem => item.dag.dag.month)/@html.displayfor(modelitem => item.dag.dag.year)</td>             </tr>         }     </table> </div> <div>     <p><input type="submit" value="volgende"/></p> </div> } 

model:

using system; using system.collections.generic; using projectii.models.domain;  namespace projectii.viewmodels { public class datamodel {     public  datamodel()     {         datums = new list<datummodel>();     }      public icollection<datummodel> datums { get; set; }     public icollection<toekomstmodel> toekomst{ get; set; } }  public class datummodel {     public datummodel()     {         dag = new opleidingdag(new datetime().date);         checked = true;     }     public datummodel(opleidingdag opldag)     {         dag = opldag;         checked = true;     }      public opleidingdag dag { get; set; }     public bool checked { get; set; } }  public class toekomstmodel {     public toekomstmodel()     {         dag = new opleidingdag(new datetime().date);     }     public toekomstmodel(opleidingdag opldag)     {         dag = opldag;     }      public opleidingdag dag { get; set; } } } 

thanks in advance help

with way construct view, modelbinder cannot differantiate between different elements in list, , doesn't recognize them items in list. try constructing view this:

@{ int datumteller = 0; }  @foreach (var item in @model.datums)  {         <tr>             <td>@html.displayfor(modelitem => @model.datums[datumteller].dag.dag.day)/@html.displayfor(modelitem => @model.datums[datumteller].dag.dag.month)/@html.displayfor(modelitem => @model.datums[datumteller].dag.dag.year)</td>             <td>@html.editorfor(modelitem => @model.datums[datumteller].checked)</td>         </tr>  datumteller++;  }  @{ int toekomstteller = 0; }  @foreach (var item in @model.toekomst)  {         <tr>             <td>@html.displayfor(modelitem => @model.toekomst[toekomstteller].dag.dag.day)/@html.displayfor(modelitem => @model.toekomst[toekomstteller].dag.dag.month)/@html.displayfor(modelitem => @model.toekomst[toekomstteller].dag.dag.year)</td>         </tr>   toekomstteller++   } 

Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -