mvc3 dropDownList的使用

DropDownList则与TextBox等控件不同,它使用的是select标记。它需要两个值:在下拉框中显示的列表,和默认选项。而自动绑定一次只能绑定一个属性,因此你需要根据需要选择是绑定列表,还是默认选项。

DropDownList扩展方法的各个重载版本“基本上”都会传递到这个方法上:

public static string DropDownList(this HtmlHelper htmlHelper,   
string name,
IEnumerable<SelectListItem> selectList,
string optionLabel,
IDictionary<string, object> htmlAttributes) {

}

如果没有指定selectList,该方法将自动绑定列表,即从ViewData中查找name所对应的值。如果提供了selectList,将自动绑定默认选项,即从selectList中找到Selected属性为true的SelectedListItem。(具体参见HtmlHelper方法的SelectInternal辅助方法)

例1:如果在Action方法中有如下代码:

  
List<SelectListItem> items = new List<SelectListItem>();
items.Add(new SelectListItem { Text = "Kirin", Value = "29" });
items.Add(new SelectListItem { Text = "Jade", Value = "28", Selected = true});
items.Add(new SelectListItem { Text = "Yao", Value = "24"});
this.ViewData["list"] = items;

在View中这样使用:

那么辅助方法将率先从ViewData中获取key为list的项,如果该项为IEnumerable类型则绑定到下拉框中,否则将抛出InvalidOperationException。由于第二个SelectListItem的Selected为true,则默认选中第二个。

例2:如果Action中代码如下:

List<SelectListItem> items = new List<SelectListItem>();   
items.Add(new SelectListItem { Text = "Kirin", Value = "29" });
items.Add(new SelectListItem { Text = "Jade", Value = "28"});
items.Add(new SelectListItem { Text = "Yao", Value = "24"});
this.ViewData["list"] = items;
this.ViewData["selected"] = 24;

<%=Html.DropDownList("selected", ViewData["list"] as IEnumerable<SelectListItem>)%>

那么辅助方法将ViewData["list"]绑定为下拉框,然后从ViewData中获取key为selected的项,并将下list中Value值与该项的值相等的SelecteListItem设为默认选中项。

以上两种方法尽管可以实现DropDownList的正确显示,但并非最佳实践。在实际项目中,我们更希望在代码中使用强类型。例如上面两例中,SelectListItem的Text和Value本来是User对象的Name和Age属性,然而上面的代码却丝毫体现不出这种对应关系。如果User列表是从数据库或其他外部资源中获得的,我们难道要用这样的方式来绑定吗?

var users = GetUsers();   
foreach (var user in users)
{
items.Add(new SelectListItem { Text = user.Name, Value = user.Age.ToString() });
}

这显然是我们所无法容忍的。那么什么是最佳实践呢?

ASP.NET MVC为DropDownList和ListBox(都在html中使用select标记)准备了一个辅助类型:SelectList。SelectList继承自MultiSelectList,而后者实现了IEnumerable。也就是说,SelectList可以直接作为Html.DropDownList方法的第二个参数。

MultiSelectList包含四个属性,分别为:

Items:用于在select标记中出现的列表,通常使用option标记表示。IEnumerable类型。

DataTextField:作为option的text项,string类型。

DataValueField:作为option的value项,string类型。

SelectedValues:选中项的value值,IEnumerable类型。

显然,作为DropDownList来说,选中项不可能为IEnumerable,因此SelectList提供了一个新的属性:

SelectedValue:选中项的value值,object类型。

同时,SelectList的构造函数如下所示:

public SelectList(IEnumerable items, string dataValueField, string dataTextField, object selectedValue)   
: base(items, dataValueField, dataTextField, ToEnumerable(selectedValue)) {
SelectedValue = selectedValue;
}

于是我们的代码变为:

var users = GetUsers();   
var selectList = new SelectList(users, "Age", "Name", "24");
this.ViewData["list"] = selectList;
<%=Html.DropDownList("list")%>

当然,你也可以使用不带selectedValue参数的构造函数重载,而在view中显式指定IEnumerable,并在ViewData或view model中指定其他与DropDownList同名的项作为默认选项。

最后让我们来回顾一下DropDownList的三种用法:

建立IEnumerable并在其中指定默认选中项。

建立IEnumerable,在单独的ViewData项或view model的属性中指定默认选中项。

使用SelectList。

 

(以上资料从网上转的)

posted @ 2012-03-27 15:15  杨伟明  阅读(3297)  评论(1编辑  收藏  举报