Ninita
Ninita

Reputation: 1249

DropDownListFor non selected and with requirer text

How can I do a @Html.DropDownListFor element non selected and with a requirer text without adding a new option element with that?

I have this code:

@Html.DropDownListFor(model => model.Description, new SelectList(ViewBag.CarsDsc, "Id", "Description"), new { @class = "iEdit"})

but I want something like this:

enter image description here

Upvotes: 0

Views: 82

Answers (1)

Mahmoud Gamal
Mahmoud Gamal

Reputation: 79969

There is an overload for the method DropDownListFor, that accepts an option value optionLabel:

The text for a default empty item. This parameter can be null.

@Html.DropDownListFor(model => model.Description, 
                      new SelectList(ViewBag.CarsDsc, "Id", "Description"), 
                      "-Please select one-",
                      new { @class = "iEdit"})

Upvotes: 0

Related Questions