ASP.NET MVC dropdown with a default empty option

James Newton-King picture James Newton-King · Feb 3, 2009 · Viewed 75k times · Source

Is there a way to include a default empty option (or with text) if there is no selected value for a dropdownlist?

Answer

tvanfosson picture tvanfosson · Feb 3, 2009

The below will prepend string.Empty to the SelectList (or IEnumerable) specified in the ViewData["Menu"] item. The select will have id and name MenuID.

<%= Html.DropDownList( "MenuID",
                      (IEnumerable<SelectListItem>)ViewData["Menu"],
                      string.Empty ) %>

Documentation: DropDownList method