0

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
}
Damon
  • 3,004
  • 7
  • 24
  • 28
Guilherme Calegari
  • 303
  • 1
  • 4
  • 9
  • 1
    You are binding a single instance of the model to the view, but expecting the post to supply a list of view models back. That's like getting your watch fixed, and expecting the store to give you 10 watches back. – asawyer Dec 10 '13 at 14:50
  • Im not sure what exactly your wanting to happen. are you wanting to return multiple records from each list? – Ryan Schlueter Dec 10 '13 at 14:54
  • Hi @RyanSchlueter, I intend to return a List. Basically I need each of tihs FlowViewModels to have the related SelectedUSer, Selected Role. Was I clear? – Guilherme Calegari Dec 10 '13 at 14:55

1 Answers1

0

You need to bind to an array/list instead of the same property over and over

Check here the solution ASP.NET MVC - Can't bind array to view model

Community
  • 1
  • 1
Paulo Correia
  • 616
  • 5
  • 15
  • Paulo Correia, could you better explain the "You need to bind to an array/list instead of the same property over and over"? Should I have a @Model List so that I'd iterate over it to properly generate the "lists" ? What should my POST method be awating for? Thanks. – Guilherme Calegari Dec 10 '13 at 21:39
  • Hi Guilherme, in you View, you have the "for" cycle that create the DropDownList. Each time you create the DropDownList, they are data binded to the same property SelectedUSer. I would transform that property in to a array, since you know the size trought ViewBag.LevelsAmount, and bind to the position of the array. something like @for(int i=0; i < ViewBag.LevelsAmount; i++) { @Html.DropDownListFor(m => m.SelectedUSer[i], Model.UserList, "SELECT") @Html.DropDownListFor(m => m.SelectedRole[i], Model.RoleList, "SELECT") } – Paulo Correia Dec 11 '13 at 10:27
  • If you check the generated HTML from your code and mine, you will see that mine are data binded to the position 0, 1, etc of the array. – Paulo Correia Dec 11 '13 at 10:27