How do I create a ListBox in ASP.NET MVC with single selection mode?
5 Answers
I am assuming you are looking for a select box visually like the ListBox, meaning with multiple rows displayed, but functionally like the DropDownList (allowing for only one selection).
It looks like there is not a particularly easy way to pull this off using ListBox. I'd suggest using Html.DropdownList, similar to this:
<%= Html.DropDownList("list1",
new Dictionary<string, object> {{"size", "5"}} ) %>
The size attribute will give the select box the look of a ListBox. Also, you will need to change your ViewData item from MultiSelectList to SelectList.
3 Comments
the below should do it: The object is translated in a list of attributes for the select element.
Html.DropDownList("list1", new Object {@rows = 5, @multiple = false} )