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

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

问题描述

我使用asp.net MVC 3。

I'm using asp.net mvc 3.

我有国家这个DropDownList的,我想添加一个占位符吧。这里是我的code:

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.

我如何设置一个占位符吗?

How do I set a placeholder for this?

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

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

推荐答案

在您的收藏ViewBag.Countries刚从名为请选择 - 集合插入在一个虚拟的记录。你应该能够迫使选择的项目,像这样一个可选的构造:

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天全站免登陆