asp.net mvc 如何为 html.dropdownlist 添加占位符 [英] asp.net mvc How to add placeholder for html.dropdownlist

查看:21
本文介绍了asp.net mvc 如何为 html.dropdownlist 添加占位符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 asp.net mvc 3.

I'm using asp.net mvc 3.

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

I have this dropdownlist of countries and I want to add a placeholder for it. Here's my code:

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

但占位符不起作用,而样式有效.

But the placeholder doesn't work, while the style works.

如何为此设置占位符?

附注.我只想使用 @Html.DropDownList,而不是 @Html.DropDownListFor

PS. I just want to use @Html.DropDownList, not @Html.DropDownListFor

推荐答案

在您的集合 ViewBag.Countries 中,只需在集合的起始位置插入一个名为-select-"的虚拟记录.您应该能够使用这样的替代构造函数强制选定项目:

In your collection ViewBag.Countries just insert a dummy record at the from of the collection with the name "-select-". You should be able to force the selected item with an alternate constructor like this:

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

这篇关于asp.net mvc 如何为 html.dropdownlist 添加占位符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆