i'm new learning mvc 5 , i'm out of ideas on one. have razor view won't render @html.hiddenfor inside loop enclosed if statement. tried removing loop, leaving if statement, , still won't render. if remove both, render properly.
any appreciated.
here's abbreviated code -- model's selectedfields
list populated in controller:
models:
public class maintmodel { public list<sourcefield> selectedfields{ get; set; } } public class sourcefield { private int id; public int id { { return id; } set { id = value; } } private int columnorder; public int columnorder { { return columnorder; } set { columnorder = value; } } }
razor code:
@using totestmgmt @model totestmgmt.models.maintmodel @using (html.beginform("insertdataset", "datasetmaint", formmethod.post)) { if (model != null && model.selectedfields!= null) { (int = 0; < model.selectedfields.count; i++) { @html.hiddenfor(model => model.insertdatasetselfields[i].id, new { id = "kevtest1" }) @html.hiddenfor(model => model.insertdatasetselfields[i].columnorder, new { id = "kevtest2" }) @html.hiddenfor(model => model.datasetsource, new { id = "datasetsourcehidden1" }) } } }
@html.hiddenfor
works current page model. mean
@model totestmgmt.models.maintmodel
also i've change logic generating id attribute because id attribute must unique on page.
@using totestmgmt @model totestmgmt.models.maintmodel @using (html.beginform("insertdataset", "datasetmaint", formmethod.post)) { if (model != null && model.selectedfields!= null) { (int = 0; < model.selectedfields.count; i++) { @html.hiddenfor(model => model.selectedfields[i].id, new { id = "kevtest" + }) @html.hiddenfor(model => model.selectedfields[i].columnorder, new { id = "kevtestcolumn" + })) } } }