Is there a way to include a default empty option (or with text) if there is no selected value for a dropdownlist?
A:
In general you add an empty value as the first item in the dropdown.
Otávio Décio
2009-02-03 04:25:39
+8
A:
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<SelectItemList>)ViewData["Menu"],
string.Empty ) %>
EDIT: This works with ASP.NET MVC RC1. Beta had slightly different ordering for the parameters. I would call that a breaking change.
tvanfosson
2009-02-03 04:39:52
Thanks. The parameter name of optionLabel for the default option threw me off :)
James Newton-King
2009-02-04 01:44:13
Geez... me too I was starting to write my own helper method... rubbish name IMHO - sounds like it would output a <label> tag next to it.
Mr Grok
2009-11-26 16:51:39