空值添加到ASP.net MVC一个DropDownList [英] Add empty value to a DropDownList in ASP.net MVC

查看:158
本文介绍了空值添加到ASP.net MVC一个DropDownList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立一个数据输入接口,并已成功地结合具有用于使用的DropDownList所以用户从$ P $对配置的值选择其数据参考表中的列

I'm building a data entry interface and have successfully bound the columns that have reference tables for their data using DropDownList so the user selects from the pre-configured values.

我现在的问题是,我不希望被默认选中的第一个值千万,我需要强制用户从列表中选择一个值,以避免错误,他们没有接那场,默认情况下一个价值分配。

My problem now is that I don't want the first value to be selected by default, I need to force the user to select a value from the list to avoid errors where they didn't pick that field and by default a value was assigned.

是否有这样做的更优雅的方式,而不是增加code,包括在列表顶部的空值后,我从数据库中得到它之前,我在我的控制器类把它传递给选择列表的构造?

Is there a more elegant way of doing this than to add code to include an empty value at the top of the list after I get it from the database and before i pass it to the SelectList constructor in my controller class?

推荐答案

在HTML辅助函数有一个'第一个空值参数作为第三个参数。

The Html helper function takes a 'first empty value' parameter as the third argument.

<%=Html.DropDownList("name",dataSource,"-please select item-")%>

这篇关于空值添加到ASP.net MVC一个DropDownList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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