且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我如何创建ASP.NET MVC单选择模式一个ListBox?

更新时间:2022-11-06 17:04:27

我假设你正在寻找一个选择框在视觉上像列表框,以显示多行的意思,但功能类似的DropDownList(允许只有一个选择) 。

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

看起来没有使用列表框拉这一关一个特别简单的方法。我建议使用Html.DropdownList,与此类似:

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"}} ) %>

size属性将会给选择框列表框的外观。此外,您还需要将ViewData的项目从MultiSelectList改变的SelectList。

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.