.NET MVC MultiSelectList and selected values
I am trying to set the selected values for a MultiSelectList but the page does not display any values as selected. I have tried every practical way to do this and still get the same results. I am currently trying to pass a list of selected objects in via the constructor. What gets me is that when I inspect MultiSelectList object, the selectedvalues property contains the items that I have passed into it using the constructor but it still does not display.
I have noticed that many people are having the same problem but I have not seen an answer that has worked for me. I have tried iterating开发者_运维知识库 through the list and setting the selected property to true, I have tried creating the multiselectlist from scratch and setting each individual items selected property and I have tried using the constructor. None of these have worked. I also found an a suggestion that the MultiSelectList property could not be named the same as the control (which I tried) and that did not work. Why is it so difficult to create a dropdown list with selected values using this framework? Am I missing something?
<label for="StatesOfPractice">States of Practice:</label>
<br />
<%= Html.ListBox("StatesOfPractice", Model.StatesOfPracticeList)%>
<br />
<%= Html.ValidationMessage("StatesOfPractice")%>
Edit 1
It appears to be happening when I instantiate the MultiSelectList. If I inspect the object in the View and open the Results View I can see that "Alaska" is not selected when I know that it should be.
http://imgur.com/eTIdH.jpg
I am instantiating like this.
new MultiSelectList(List<GenericDataContract>, "Code", "Description", List<GenericDataContract>);
The GenericDataContract is simply a class with two properties, Code and Description. The first List is all of the states, the second List is my selected states.
Grrrrrr, I found it. Apparently, the selected items can only be a list of strings. Once I modified my code to pass this in it selected the values.
OK, this one really confused me but I think I've got it figured out.
The problem I had getting the MultiSelectList
to display with defaults selected was that I was using @Html.DropDownListFor(...)
to display the multi select list instead of @Html.ListBoxFor(...)
. Confusingly, DropDownListFor
will display a multi select list if you specify the HTML attributes to force it to do so, but the default values will not work, because it's only intended to have one default - the following will therefore not have any defaults 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 defaults of "aaa" and "ccc" as selected:
@Html.ListBoxFor(model => model.MyStrings, (MultiSelectList)ViewData["MyStringsDropdown"], new { size = "10", multiple = "multiple" })
But here's the real kicker; ASP.NET sometimes seems cache the type of the control, so you can replace the DropDownListFor
with the ListBoxFor
and it will still not pre-select any defaults until you restart the IIS worker process. I find that really strange, and it threw me for a while.
I believe that it pulls the selected items from the model, not from the list of items. Make sure that your StatesOfPractice
model property is populated with the items that you want selected. That is, the StatesOfPracticeList
model property provides the set of items used to populate the list. The model property StatesOfPractice
which will be the set of selected property values when the form is posted, should also contain the default selected values when the view is rendered. Under the hood it uses the HtmlHelper GetModelStateValue
method to find the selected value(s) for any select list. I presume this is done so that it is simpler to maintain the selected values when there is a validation error and the form is rerendered with the model state errors.
精彩评论