找不到为 MVC 5 DropDownListFor 添加占位符的方法
Cannot find a way to add a placeholder for an MVC 5 DropDownListFor
我已经尝试在网上搜索并尝试使用我的代码进行不同的操作。我知道如何为文本框添加占位符,但如何为 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" } })
第三次重载可以是 "placeholder" (optionLabel)。
select 框没有像文本输入那样的 "placeholder"。
我知道我的回答来得太晚了,但由于我一直在努力解决这个问题,所以我想与您分享我的解决方案。
在我工作的项目中,我正在使用 bootstrap 并且我希望在未选择任何内容时显示占位符,并且如果选择了一个项目能够与占位符一起保存(允许在分贝)
@Html.DropDownListFor(model => model.CustomerID,
new SelectList(ViewBag.Customers, "CustomerID", "Email"),
"-- Please Select --",
new { htmlAttributes = new { @class = "form-control", data_placeholder= "-- Please Select --" } })
我还使用 js 将样式添加到我的下拉列表中:
function initCustomerDropdown() {
$("#CustomerID").addClass('select2_demo_3 form-control');
$("#CustomerID").select2({
allowClear: true,
width: '100%'
});
}
So when no item was selected, "-- Please Select --" was displayed and when an item is selected the allow clear 属性 allowed me to clear the search displaying the placeholder and允许我保存 null。
希望这会有所帮助。干杯!
我已经尝试在网上搜索并尝试使用我的代码进行不同的操作。我知道如何为文本框添加占位符,但如何为 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" } })
第三次重载可以是 "placeholder" (optionLabel)。
select 框没有像文本输入那样的 "placeholder"。
我知道我的回答来得太晚了,但由于我一直在努力解决这个问题,所以我想与您分享我的解决方案。 在我工作的项目中,我正在使用 bootstrap 并且我希望在未选择任何内容时显示占位符,并且如果选择了一个项目能够与占位符一起保存(允许在分贝)
@Html.DropDownListFor(model => model.CustomerID,
new SelectList(ViewBag.Customers, "CustomerID", "Email"),
"-- Please Select --",
new { htmlAttributes = new { @class = "form-control", data_placeholder= "-- Please Select --" } })
我还使用 js 将样式添加到我的下拉列表中:
function initCustomerDropdown() {
$("#CustomerID").addClass('select2_demo_3 form-control');
$("#CustomerID").select2({
allowClear: true,
width: '100%'
});
}
So when no item was selected, "-- Please Select --" was displayed and when an item is selected the allow clear 属性 allowed me to clear the search displaying the placeholder and允许我保存 null。
希望这会有所帮助。干杯!