Search code examples
c#.netasp.net-mvcdropdownviewbag

How to pass List<string> to dropdown?


I have a list as follows:

List<string>WeekEnding={'10/07/2018','11/11/2018','01/21/2018'};

I want to pass it to the dropdown with name='10/07/2018' value='10/07/2018'

my dropdown is

  @Html.DropDownList("WeekEnding", null, new { Id = "WeekEnding", style = "width:50px;", @class = "form-control js-select", @size = "2" , required = "required" })  

Solution

  • You can use like this

    @model List<string>
    @Html.DropDownList(
        "WeekEnding", 
        new SelectList(
            Model.Select(x => new { Value = x, Text = x }),//you have to pass data as model. If you use another way you must change this line. 
            "Value",
            "Text"
        ),
        new { Id = "WeekEnding", style = "width:50px;", @class = "form-control js-select", @size = "2" , required = "required" }
    )