<%=Html.ListBox("respons...">

MVC ModelBind ListBox with many options

How to send ListBox to ModelBinder?

<%=Html.Hidden("response.Index",index)%>
<%=Html.ListBox("response[index].ChoiceID",
                new MultiSelectList(gc.choice,"ChoiceID","ChoiceText") )%>

'gc.choice' is a list

I can get the selected fisrt value for the model, but not the second choice presumably because I cannot change the index.

+1
source share
2 answers

I decided it a little differently ...

[Model]
public IEnumerable<string> SelectedStores { get; set; }

[View]
<%= Html.ListBox("SelectedStores", 
    (MultiSelectList)ViewData["Stores"], 
    new { size = "8" }) %>

[Controller]
ViewData["Stores"] = 
    new MultiSelectList(StoreItems, "Value", "Text", model.SelectedStores);

So, the model has an IEnumberable that will be populated with user selections. The view displays a ListBox with a MultiSelectList, and the controller jumps to SelectedStores from the model when it creates a MultiSelectList.

+3
source

I built a presentation class SamplePresentationModelthat has a MultiSelect element userList. Then let IEnumerable<User> allUser- a list of options. I use

  View(new SamplePresentationModel(){ userList = new MultiSelectList(allUsers, 
    "UserId", 
    "UserName", 
    allUsers.Select(user => user.UserID))});

MultiSelection .

   <label for="userList">users:</label>
    <%= Html.ListBox("usersList", Model.userList)%>

POST :

IEnumerable<int> selectedUserIDs = Request["usersList"].Split(new Char[] { ',' }).Select(idStr => int.Parse(idStr));

, !

+1

Source: https://habr.com/ru/post/1794261/


All Articles