asp.net mvc 4 - Getting invalid ID and Name in mvc razor when model has navigation property? -


this razor code in view.

int pmtcount = 0; if (item.projectmaterialtypes != null) {                                        @foreach (var x in item.projectmaterialtypes) { pmtcount = pmtcount + 1;                             @html.hiddenfor(model =>model.projecttypes[ptcount - 1].projectmaterialtypes.tolist()[pmtcount - 1].projectmaterialtypeid, new { value = x.projectmaterialtypeid}) @html.hiddenfor(model => model.projecttypes[ptcount - 1].projectmaterialtypes.tolist()[pmtcount - 1].materialtypes.name, new { value = x.materialtypes.name }) <label>@html.checkboxfor(model => model.projecttypes[ptcount - 1].projectmaterialtypes.tolist()[pmtcount - 1].status, new { @onclick = "togglesingle('smt-" + x.materialtypeid + "')" }) @x.materialtypes.name</label> } } 

populating id , name on browser. enter image description here

but getting null object in controller submitting form, please let me know there way solve problem?

simply changed icollection list in model library.in way resolved issue. html helper generate property name when use list instead of icollection in entity framework model class.

from

public virtual icollection<projectmaterialtype> projectmaterialtypes { get; set; } 

changed

public virtual list<projectmaterialtype> projectmaterialtypes { get; set; } 

Comments

Popular posts from this blog

javascript - Unusual behaviour when drawing lots of images onto a large canvas -

how can i manage url using .htaccess in php? -

javascript - Chart.js - setting tooltip z-index -