Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add empty value to a DropDownList in ASP.net MVC

I'm building a data entry interface and have successfully bound the columns that have reference tables for their data using DropDownList so the user selects from the pre-configured values.

My problem now is that I don't want the first value to be selected by default, I need to force the user to select a value from the list to avoid errors where they didn't pick that field and by default a value was assigned.

Is there a more elegant way of doing this than to add code to include an empty value at the top of the list after I get it from the database and before i pass it to the SelectList constructor in my controller class?

like image 660
jvanderh Avatar asked Jun 02 '09 22:06

jvanderh


1 Answers

The Html helper function takes a 'first empty value' parameter as the third argument.

<%=Html.DropDownList("name",dataSource,"-please select item-")%>
like image 80
Ropstah Avatar answered Sep 21 '22 14:09

Ropstah