Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot find a way to add a placeholder for an MVC 5 DropDownListFor

I've tried searching the web and trying out different things with my code. I know how to add a placeholder for a textbox, but how about adding one for an MVC 5 dropdownlistfor?

I have the following code, but will not put the placeholder in the dropdownlist.

@Html.DropDownListFor(model => model.CustomerID, new SelectList(ViewBag.Customers, "CustomerID", "Email", null, new { htmlAttributes = new { @class = "form-control", @placeholder = "-Select-" } }))

What would be the syntax I would need to accomplish this?

like image 743
sagesky36 Avatar asked Feb 12 '15 02:02

sagesky36


1 Answers

Try this:

@Html.DropDownListFor(model => model.CustomerID, 
    new SelectList(ViewBag.Customers, "CustomerID", "Email"), 
    "-- Please Select --", 
    new { htmlAttributes = new { @class = "form-control" } })

3rd overload can be the "placeholder" (optionLabel).

A select box doesnt have a "placeholder" like text inputs do.

like image 64
mnsr Avatar answered Oct 12 '22 14:10

mnsr