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

标签 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-" } }))

完成此操作所需的语法是什么?

最佳答案

试试这个:

@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/

相关文章:

asp.net - 创建密码字段@html.TextBoxFor

asp.net-mvc - Kendo mvc 网格打印

c# - 如何获得 <0;9> 范围内的 4 个唯一随机数?

c# - ValueTypes 如何从 Object (ReferenceType) 派生并仍然是 ValueTypes?

javascript - 在 C# 中使用 AJAX 绑定(bind) gridview 时报告“未定义”

c# - 如何创建 Async ActionResult 以创建用于以下轮询逻辑的 async-await 模式?

c# - 读取string intern pool的内容

c# - 与 jQuery UI 对话框交互后如何继续使用 asp.net 服务器端按钮单击方法?

javascript - asp :HiddenField fails to be persisted across postback 的 jQuery 更新

c# - 从 asp.net 打开任何文件