asp.net mvc如何为html.dropdownlist添加占位符

时间:2013-04-18 02:20:27

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

我正在使用asp.net mvc 3。

我有这个国家/地区的下拉列表,我想为它添加一个占位符。这是我的代码:

@Html.DropDownList("country",
     new SelectList(ViewBag.countries as System.Collections.IEnumerable,
     "name", "name"), new { @class="chzn-select", @placeholder="-select-",
     @style="width:160px;" } )

但占位符不起作用,而风格有效。

如何为此设置占位符?

PS。我只想使用@Html.DropDownList,而不是@Html.DropDownListFor

10 个答案:

答案 0 :(得分:9)

你可以试试这个:

@Html.DropDownList("country", new SelectList(ViewBag.countries), "-select- ", new { @class="chzn-select", @style="width:160px;" })

答案 1 :(得分:5)

在您的集合ViewBag.Countries中,只需在集合的from处插入一个名为“-select-”的虚拟记录。您应该能够使用如下的替代构造函数强制选定的项目:

@Html.DropDownList("country",
     new SelectList(ViewBag.countries as System.Collections.IEnumerable,
     "name", "name", "-select-"), new { @class="chzn-select", @style="width:160px;" } )

答案 2 :(得分:3)

@Html.DropDownListFor(m => m.SelectedProductIDs, 
Model.AvaliableProducts.ToSelectList("ID","Name"),
                       new { @class = "chzn-select", 
                            data_placeholder="Please select a product" })...

请参阅更多详情:http://code.stylenet.ca/mvc3-data-placeholder-html5-attribute/

答案 3 :(得分:2)

涉及jQuery的快速且肮脏的解决方案。

不是在列表的开头添加虚拟项,而是在新选项之前添加一个已禁用的选项。主要优点是您无需弄乱列表中的虚拟项目,最重要的是,您将无法在页面中选择该虚拟项目 strong>:

@Html.DropDownList("yourName", yourSelectList, new { @class = "form-control select-add-placeholder" })

然后在某处:

$(".select-add-placeholder").prepend("<option value='' disabled selected>Select an option...</option>");

外观如下:

enter image description here

答案 4 :(得分:1)

我可以看到答案并不容易。 我开发了一个简单的解决方案,但它提供了一些功这适用于 DropDownList DropDownListFor

首先在任何地方创建一个扩展类。 (必须是静态的静态方法) 然后添加以下扩展方法

public static MvcHtmlString DropDownList(this HtmlHelper htmlHelper,string name, IEnumerable<SelectListItem> selectList, string optionLabel, bool disableLabel)
{
    MvcHtmlString mvc = htmlHelper.DropDownList(name, selectList, optionLabel);
    if (disableLabel)
    {
        string disabledOption = mvc.ToHtmlString();
        int index = disabledOption.IndexOf(optionLabel);
        disabledOption = disabledOption.Insert(index - 1, " disabled");

        return new MvcHtmlString(disabledOption);
    }
    else
    {
        return mvc;
    }
}

现在请注意,此扩展程序仅添加一个新属性 disableLabel 这将检查您是否要禁用标签。

现在你让我们去观看 首先声明您要使用已创建的扩展类 例如:@using WF.Infrastructure.Extension;

现在你喜欢它:@Html.DropDownList("Unidade", lstUnidade, "Text of Option Disabled",true)

注意:如果您希望它与DropDownListFor配合使用,只需使用此签名为您的Extension类添加另一种扩展方法:

public static MvcHtmlString DropDownListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectList, string optionLabel,bool disableLabel)

PS:如果在像我这样的独立项目中声明扩展方法,则需要添加程序集(System.Web.Mvc,System.Web) 在您的扩展类中,您需要声明:

using System.Web.Mvc;
using System.Web.Mvc.Html;

答案 5 :(得分:0)

也许,如果需要,请尝试以下操作:

@Html.DropDownList("country",null,"-SELECT-",new { @class="chzn-select",@style="width:160px;" })

答案 6 :(得分:0)

@ Html.DropDownList(“ MetadataId”,(MultiSelectList)ViewData [“ category”],“所有类别”,新{@class =“ chk_dropdown d-none”,@id =“ categories”,多个=“多个“})

答案 7 :(得分:-1)

试试这个

@Html.DropDownListFor(m => m.SelectedProductIDs, Model.AvaliableProducts.ToSelectList("ID","Name"), new { @class = "chzn-select", data-placeholder="Please select a product" })

或者

@Html.DropDownListFor(model => model.SelectedItemID, new SelectList(Model.employeeList, "Value", "Text"), new Dictionary<string, object> { { "data-placeholder", "Choose a sandbox..." }, { "class", "chzn-select" }, { "style", "width:200px;" } })

答案 8 :(得分:-1)

最好的方式

@Html.DropDownListFor(m => m.SelectedProductIDs,
                      Model.AvaliableProducts.ToSelectList("ID","Name"),
                      "Please select a product",
                      new { @class = "chzn-select"})

答案 9 :(得分:-1)

{{1}}