2011-10-20 28 views

Odpowiedz

19

Chcesz użyć MultiSelectList zamiast który ma konstruktora do swoich potrzeb:

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

okej ... czy to będzie działać z @ Html.DropDownList() w moim kod brzytwy? – Mariah

+2

Nie, będziesz musiał użyć Html.ListBox ... macierzyste listy rozwijane HTML nie obsługują wielokrotnego wyboru. Zobacz http://blog.garypretty.co.uk/index.php/2010/02/26/multi-select-list-box-in-asp-net-mvc/ –

+1

@RobertLevy możesz użyć DropDownList w ten sposób: @ Html.DropDownList ("yourName", yourMultiSelectList, new {multiple = ""}}) – Matus

14

Przykład:

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 }); 
Powiązane problemy