2011-10-20 51 views

回答

19

你想用MultiSelectList,而不是它有一個構造函數,以滿足您的需求:

public MultiSelectList(
    IEnumerable items, 
    string dataValueField, 
    string dataTextField, 
    IEnumerable selectedValues 
) 
+0

好的...將在我的剃鬚刀代碼中使用@ Html.DropDownList()嗎? – Mariah

+2

不,你將不得不使用Html.ListBox ...本地HTML下拉列表不支持多選。見http://blog.garypretty.co.uk/index.php/2010/02/26/multi-select-list-box-in-asp-net-mvc/ –

+1

@RobertLevy你可以像這樣使用DropDownList: @ Html.DropDownList(「yourName」,yourMultiSelectList,new {multiple =「」}) – Matus

14

例子:

class Person 
{ 
    int Id{ get; set; } 
    string Name { get; set; } 
} 

... 

var people = new List<Person>() 
{ 
    new Person{ Id = 1, Name = "Steve" }, 
    new Person{ Id = 2, Name = "Bill" }, 
    new Person{ Id = 3, Name = "John" }, 
    new Person{ Id = 4, Name = "Larry" } 
} 
SelectList List = new MultiSelectList(people, "Id", "Name", new[]{ 2, 3 }); 
相關問題