2012-04-27 13 views
6

Sapete con quale facilità posso associare il contenuto di un array di stringhe a DropDownList in vista per Rasoio MVC?Associazione di un array di stringhe a DropDownList in MVC Razor

public static string[] AgeRagne = new string[] { "Sun", "Mon", "Tues", "Wed" }; 

UPDATE: Sotto codice ha funzionato.

@Html.DropDownListFor(
    model => model.Filter.AgeRange, 
    new SelectList(Extensions.AgeRange, Model.Filter.AgeRange), 
    new { @class = "search-dropdown", name = "ageRange" } 
) 
+0

sto solo prendendo una ipotesi davvero selvaggia .. ma intendevi 'AgeRange' not' AgeRagne'? => => '.. stringa [] AgeRagne = ...' –

risposta

4

un modo non molto bello ma veloce sarebbe quello di farlo :):

<select name="dowList" id="dowList"> 
    @{string[] AgeRagne = new string[] { "Sun", "Mon", "Tues", "Wed" };} 
    @foreach (var dow in AgeRagne) 
    { 
     <option value="@dow">@dow</option> 
    } 
</select> 

tho un HtmlHelper sarebbe la migliore soluzione stabile a lungo termine.

19

Creare un SelectList con la matrice e passarlo alla vista:

SelectList list = new SelectList(AgeRagne); 
ViewBag.myList = list; 

Poi, nel tuo vista, utilizzare Html.DropDownlist:

@Html.DropDownList("myList", ViewBag.myList as SelectList) 

Questo è tutto

+0

Funziona perfettamente :) salvato la mia giornata – Zeeshan

+0

semplice e veloce. uno dei modi più semplici per utilizzare un elenco a discesa –