无法找到为MVC 5 DropDownListFor添加占位符的方法

无法找到为MVC 5 DropDownListFor添加占位符的方法

问题描述:

我尝试过搜索网络,并尝试使用我的代码进行其他操作.我知道如何为文本框添加一个占位符,但是如何为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?

尝试一下:

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

第三个重载可以是占位符"(optionLabel).

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

选择框没有像文本输入一样的占位符".

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