2008-12-09 32 views

回答

33

我假設你正在尋找一個像列表框一樣的選擇框,這意味着顯示多行,但功能上與DropDownList類似(只允許一個選擇)。

看起來好像沒有一種特別簡單的方法可以使用ListBox將其關閉。我建議使用Html.DropdownList,與此類似:

<%= Html.DropDownList("list1", 
    new Dictionary<string, object> {{"size", "5"}}) %> 

size屬性將使選擇框成爲ListBox的外觀。此外,您需要將您的ViewData項目從MultiSelectList更改爲SelectList。

+0

順便說一句,html規範說要使用SIZE而不是ROWS。也許他們都工作我不知道。 – 2008-12-09 05:09:28

+2

我認爲它需要是新的{size = 5} – 2008-12-09 05:10:19

-2

下面應該這樣做:對象被轉換爲select元素的屬性列表。

Html.DropDownList( 「列表1」,新的對象{@rows = 5,@multiple = FALSE})

2

下面的代碼工作對我來說,

<%= Html.DropDownList( 「列表1」 ,lstItem,new {@size = 5})%> 其中lstItem表示SelectListItem的列表

1

最佳解決方案是在這裏。

<script type="text/javascript"> 
    $(document).ready(function() { 
     $('select').removeAttr('multiple'); 
    }); 
</script> 
3

MVC5.cshtml

@Html.DropDownList("PropertyID", null, htmlAttributes: new {size=5, @class="form-control" }) 

控制器

ViewBag.PropertyID = new SelectList(db.EntityItems); 
相關問題