Is there a simple way to add a "--Please select--" default option to a DropDownListFor in MVC 3?
The default value of the select element can be set by using the 'selected' attribute on the required option. This is a boolean attribute. The option that is having the 'selected' attribute will be displayed by default on the dropdown list.
DropDownListFor will automatically select the selected value by using the specified property: // Will select the item in model. Equipments that matches Model. EquipmentId @Html.
SelectListItem is a class which represents the selected item in an instance of the System. Web. Mvc.
So, I did something like this:
@Html.DropDownListFor(model => model.Dessert, new SelectList(Model.AvailableDesserts, "DessertID", "DessertName"), "---Select A Dessert ---")
Seems to work pretty well. Dessert
in my viewmodel is the one selected by the user. AvailableDesserts
is a collection of ones to pick from. Hope that helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With