asp.netasp.net-mvcmvccontribfluenthtml

Create select list with first option text with mvccontrib?


Trying to create a select list with a first option text set to an empty string. As a data source I have a List of a GenericKeyValue class with properties "Key" & "Value". My current code is as follows.

                <%= this.Select(x => x.State).Options(ViewData[Constants.StateCountry.STATES] as IList<GenericKeyValue>, "Value", "Key").Selected(Model.State) %>

This gets fills the select list with states, however I am unsure at this point of an elegant way to get a first option text of empty string.


Solution

  • "Trying to create a select list with a first option text set to an empty string." The standard way isn't fluent but feels like less work:

    ViewData[Constants.StateCountry.STATES] = SelectList(myList, "Key", "Value");
    

    in the controller and in the view:

    <%= Html.DropDownList(Constants.StateCountry.STATES, "")%>