【发布时间】:2015-09-22 21:26:10
【问题描述】:
我无法放置标题为“请选择”的占位符,所以我只是将它作为一个项目放在我的下拉列表中,但现在可以选择“请选择”,这有点麻烦。有没有办法为下面的下拉列表设置一个占位符。
<div class="form-group">
@Html.LabelFor(model => model.Type, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.Type, new List<SelectListItem>
{ new SelectListItem{Text="Please select type"},
new SelectListItem{Text="Book"},
new SelectListItem{Text="Book chapter"},
new SelectListItem{Text="Journal article"},
new SelectListItem{Text="Conference"}}, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Type, "", new { @class = "text-danger" })
</div>
</div>
【问题讨论】:
-
选择一个“占位符”是没有意义的。正确的方法是使用接受
optionLabel的DropDownListFor()的重载,并删除集合中的第一个SelectListItem-@Html.DropDownListFor(m => m.Type, new List<SelectListItem> {...}, "Please Select", new { @class = "form" }),它创建第一个带有null值的选项
标签: html asp.net-mvc drop-down-menu placeholder