Associating a String Array with a DropDownList in MVC Razor

Do you know how easy it is for me to associate the contents of a string array with a DropDownList to view MVC Razor?

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

UPDATE: Below is the code.

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

Not a very nice, but quick way: 1):

 <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 htmlhelper will be the best long-term stable solution.

+4
source

Create a SelectList with your array and pass it to your view:

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

Then, in your opinion, use Html.DropDownlist :

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

What all

+19
source

Source: https://habr.com/ru/post/914306/


All Articles