Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a ListBox in ASP.NET MVC with single selection mode?

Tags:

asp.net-mvc

How do I create a ListBox in ASP.NET MVC with single selection mode?

like image 826
Todd Smith Avatar asked Dec 09 '08 01:12

Todd Smith


People also ask

How can add listbox in ASP NET MVC?

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.

What is select list in MVC?

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.


1 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.

like image 120
Jeffrey Meyer Avatar answered Oct 03 '22 01:10

Jeffrey Meyer