I have been trying to solve it for two entire days since then. I basically have already read all the StackOverflow's topics related to this situation, but none of them were capable of solving my issue (quiet disappointed with Razor / MVC: too raw/primitive).
First things first: I am due to build a workflow definition page. This definition is composed by a dynamic list of: approval level, user and role.
View Model
public class FlowViewModel
{
public IList<SelectListItem> UserList {get; set;}
public IList<SelectListItem> RoleList {get; set;}
public IEnumerable<SelectListItem> SelectedUser {get; set}
public IEnumerable<SelectListItem> SelectedRole {get; set}
}
Controller - Index (GET)
public ActionResult Index()
{
FlowViewModel flowViewModel = new FlowViewModel();
//Logic that gathers the user list and role list from database and set
//the underlying lists (FlowViewModel.UserList and FlowViewModel.RoleList).
return View(flowViewModel)
}
View
@model App.Models.FlowViewModel
@Html.BeginForm("CreateFlow", "Flow")
{
@Html.DropDownListFor(m => m.SelectedUSer, Model.UserList, "SELECT")
@Html.DropDownListFor(m => m.SelectedRole, Model.RoleList, "SELECT")
}
Controller - POST
public ActionResult CreateFlow(FlowViewModel flowVM)
{
//whatever
}
By doing so, I properly recieve the selected index of the given lists (within the FlowViewModel).
THE MAIN QUESTION:
- How should I properly render several DropDowns so that I could retrieve a list containing: Level, SelectedUser, SelectedRole.
e.g:
VIEW
@Html.BeginForm("CreateFlow", "Flow")
{
@for(int i; i < ViewBag.LevelsAmount; i++)
{
@Html.DropDownListFor(m => m.SelectedUSer, Model.UserList, "SELECT")
@Html.DropDownListFor(m => m.SelectedRole, Model.RoleList, "SELECT")
}
<input type="submit">OK</input>
}
CONTROLLER (POST)
public ActionResult CreateFlow(IList<FlowViewModel> flowVMList)
{
//whatever
}