2011-10-20 8 views

risposta

19

si desidera utilizzare MultiSelectList invece, che ha un costruttore per soddisfare le vostre esigenze:

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

okay ... funzionerà con @ Html.DropDownList() nel codice del rasoio? – Mariah

+2

no, dovrai usare Html.ListBox ... Gli elenchi a discesa HTML nativi non supportano la selezione multipla. Vedi http://blog.garypretty.co.uk/index.php/2010/02/26/multi-select-list-box-in-asp-net-mvc/ –

+1

@RobertLevy puoi usare DropDownList come questo: @ Html.DropDownList ("yourName", yourMultiSelectList, new {multiple = ""}) – Matus

14

Esempio:

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 }); 
Problemi correlati