tags:

views:

2118

answers:

3

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
+1  A: 

I believe this post answers your question.

Peter McGrattan
+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
Thanks. The parameter name of optionLabel for the default option threw me off :)
James Newton-King
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