In MVC, SelectList derives from MultiSelectList. I can't tell a difference between them though. For both, I have to tell it to select multiple items (I expected to not have to do this since the name has "multi" in it).
If you substitute "SelectList" with "MultiSelectList" in the code below, it will generate the same HTML:
<%
var leftSelectList = new SelectList(Model.LeftSide,"Key","Value");
var attrs = new SortedDictionary<string, object> {{"class", "ui-widget"}};
MvcHtmlString disabledStyle = MvcHtmlString.Create(Html.Encode("'width:50px;'"));
attrs.Add("style", disabledStyle);
attrs.Add("multiple", "multiple");
attrs.Add("size", "5"); /*-- how many items to show--*/
var leftItems = Html.DropDownList("ddlLeftItems", leftSelectList, attrs); %>
<%= leftItems.ToHtmlString()%>
The generated HTML is:
<select class="ui-widget" id="ddlLeftItems" multiple="multiple" name="ddlLeftItems" size="5" style="&#39;width:50px;&#39;">
<option value="1">A</option>
<option value="2">B</option>
<option value="3">C</option>
<option value="5">E</option>
<option value="9">I</option>
</select>
So, which one should I use? Thank you.
MultiSelectList is used with
Html.ListBox
andHtml.ListBoxFor
methods. These helpers generate this: HTML select multiple Attribute