我已经尝试搜索网络并尝试使用我的代码进行不同的操作。我知道如何为文本框添加占位符,但如何为 MVC 5 下拉列表添加占位符呢?
我有以下代码,但不会将占位符放在下拉列表中。
@Html.DropDownListFor(model => model.CustomerID, new SelectList(ViewBag.Customers, "CustomerID", "Email", null, new { htmlAttributes = new { @class = "form-control", @placeholder = "-Select-" } }))
完成此操作所需的语法是什么?
最佳答案
试试这个:
@Html.DropDownListFor(model => model.CustomerID,
new SelectList(ViewBag.Customers, "CustomerID", "Email"),
"-- Please Select --",
new { htmlAttributes = new { @class = "form-control" } })
第三个重载可以是“占位符”(optionLabel)。
选择框不像文本输入那样有“占位符”。
关于c# - 找不到为 MVC 5 DropDownListFor 添加占位符的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28468499/