.NET MVC MultiSelectList and Selected Values

I am trying to set the selected values ​​for the MultiSelectList, but the page does not display the values ​​selected. I tried every practical way to do this and still get the same results. I am currently trying to pass a list of selected objects through a constructor. I'm interested in the fact that when I check the MultiSelectList object, the selectedvalues ​​property contains elements that I passed to it using the constructor, but it still does not show.

I noticed that many people have the same problem, but I did not see the answer that worked for me. I tried to iterate through the list and set the selected property to true, I tried to create a list of multiselects from scratch and set the selected property for each individual element, and I tried to use the constructor. None of them worked. I also found a suggestion that the MultiSelectList property cannot be called the same as the control (which I tried), and this did not work. Sincerely, why is it so difficult to create a drop-down list with selected values ​​using this structure? Did I miss something?

State Practice:

<% = Html.ListBox ("StatesOfPractice", Model.StatesOfPracticeList)%>

<% = Html.ValidationMessage ("StatesOfPractice")%>

EDIT NO. 1

It seems to happen when I create an instance of MultiSelectList. If I examine the object in the view and open the view of the results, I see that Alaska is not selected when I know what it should be.

http://imgur.com/eTIdH.jpg

I create an instance this way.

new MultiSelectList (list, "code", "description", "list");

GenericDataContract is just a class with two properties: Code and Description. The first list is all the states, the second list is my selected states.

+7
selected asp.net-mvc multi-select
source share
3 answers

I believe that it pulls the selected elements from the model, and not from the list of elements. Make sure your StatesOfPractice model property is StatesOfPractice with the items you want to select. That is, the StatesOfPracticeList model property provides a set of elements used to populate the list. The model StatesOfPractice property, which will be the set of selected property values ​​when the form is published, should also contain the default values ​​selected when rendering the view. Under the hood, it uses the HtmlHelper GetModelStateValue method to search for selected values ​​for any select list. I assume that this is done in such a way that it is easier to maintain the selected values ​​when there is a validation error, and the form is re-viewed with model state errors.

+1
source share

Grrrrrr, I found it. Apparently, the selected items can only be a list of strings. As soon as I changed my code to pass this, he selected the values.

tvanfosson - thanks for the help and quick response. very grateful!

+8
source share

Well, that really confused me, but I think I get it.

The problem I was getting a MultiSelectList for display with selected by default was that I used @Html.DropDownListFor(...) to display a multiple choice list instead of @Html.ListBoxFor(...) . Vaguely, DropDownListFor display a list with multiple choices if you specify HTML attributes to force it to do this, but the defaults will not work, as it is intended for only one default value - therefore, therefore, the default values ​​will not be selected

 List<string> myStrings; myStrings = new List<string>(); myStrings.Add("aaa"); myStrings.Add("bbb"); myStrings.Add("ccc"); ViewData["MyStringsDropdown"] = new MultiSelectList( myStrings.Select(abc => new { StringId = abc, Title = abc + "Title" }), "StringId", "Title", myStrings.Where(str => str == "aaa" || str == "ccc").ToList() ); ... @Html.DropDownListFor(model => model.MyStrings, (MultiSelectList)ViewData["MyStringsDropdown"], new { size = "10", multiple = "multiple" }) 

However, the following will display the default values ​​"aaa" and "ccc" as selected:

 @Html.ListBoxFor(model => model.MyStrings, (MultiSelectList)ViewData["MyStringsDropdown"], new { size = "10", multiple = "multiple" }) 

But here is a real kicker; ASP.NET sometimes seems like a control type cache, so you can replace DropDownListFor with ListBoxFor , and it still won’t pre-select the default values ​​until you restart the IIS workflow. I find it really strange, and it left me for a while.

+3
source share

All Articles