【问题标题】:How to put a placeholder for a dropdown list如何为下拉列表放置占位符
【发布时间】: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>

【问题讨论】:

  • 选择一个“占位符”是没有意义的。正确的方法是使用接受optionLabelDropDownListFor() 的重载,并删除集合中的第一个SelectListItem - @Html.DropDownListFor(m =&gt; m.Type, new List&lt;SelectListItem&gt; {...}, "Please Select", new { @class = "form" }),它创建第一个带有null 值的选项

标签: html asp.net-mvc drop-down-menu placeholder


【解决方案1】:

DropDownListFor helper 的重载 4、5 和 6 都包含一个参数 optionLabel,可用于您的目的。

【讨论】:

  • 您介意解释一下重载 4、5 和 6 的含义吗?抱歉,我对 MVC 很陌生
  • 看看这里:msdn.microsoft.com/en-us/library/vstudio/…。键入 Html.DropDownListFor 时,您应该得到一个可能的参数输入列表。第 4、5 和 6 项应具有字符串类型的 optionLabel 参数。
猜你喜欢
  • 2016-01-22
  • 1970-01-01
  • 2014-05-14
  • 1970-01-01
  • 2020-01-18
  • 2014-10-27
  • 1970-01-01
  • 1970-01-01
  • 2019-01-26
相关资源
最近更新 更多