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

sag*_*y36 8 c# asp.net asp.net-mvc razor

我试过在网上搜索并尝试使用我的代码进行不同的操作.我知道如何为文本框添加占位符,但如何为MVC 5下拉列表添加一个?

我有以下代码,但不会将占位符放在下拉列表中.

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

我需要的语法是什么?

jzm*_*jzm 25

试试这个:

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

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

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