I want to create DropDownLists dynamically out of a List, which supplies the SelectList and a field where to save the selection.
public class ViewModel
{
public List<Material_Select> materialSelect { get; set; }
}
public class Material_Select
{
public SelectList selectList { get; set; }
public int MaterialId { get; set; }
}
In the view, I want to loop through the materialSelect List and create the DropDownLists dynamically.
Something like this:
int count = 0;
foreach (var item in Model.materialSelect)
{
count++;
<div class="editor-label">
@Html.LabelFor(model => model.materialSelect)
</div>
<div class="editor-field">
@Html.DropDownListFor(item.MaterialId, item.selectList)
</div>
}
At the HttpPost ActionResult I need to get the selected values. Does anyone has an idea how to solve this?
foreach
loop to generate form controls for a collection - refer this answer for an explanation) – Catacaustic