Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC dropdown with a default empty option

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

like image 645
James Newton-King Avatar asked Feb 03 '09 04:02

James Newton-King


People also ask

How do you make a dropdown empty?

best way is to use $("#dropdownid"). html(''); it makes the value null. This is the only answer here that works when the select does not have a blank option, and in cases where such an option is not desired. The only other way is to add the blank option, as Andy did.

How do I remove a drop down list in HTML?

To remove the options of an HTML element of select , you can utilize the remove() method: function removeOptions(selectElement) { var i, L = selectElement. options.


1 Answers

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

like image 172
tvanfosson Avatar answered Oct 01 '22 18:10

tvanfosson