How do I create a ListBox in ASP.NET MVC with single selection mode?
So open your visual studio and create new MVC web application. First step is to create entity data model using ADO.NET entity framework. Right click on model and add → add new item, under the visual C# data select ADO.NET entity data model. Provide some name, we have given EmployeeDataModel.
SelectList(IEnumerable, String, String, String, Object, IEnumerable, IEnumerable) Initializes a new instance of the SelectList class by using the specified items for the list, the data value field, the data text field, the data group field. the selected value, the disabled values, and the disabled groups.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With