zoukankan      html  css  js  c++  java
  • ASP.NET MVC中DropDownList的使用

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

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

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

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

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

    在View中这样使用:

    代码
    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; 

    <%=Html.DropDownList("list")%>那么辅助方法将率先从ViewData中获取key为list的项,如果该项为IEnumerable<SelectedListItem>类型则绑定到下拉框中,否则将抛出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

      View中的代码如下: 

    <%=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<SelectListItem>。也就是说,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; 
     
    view里直接引用: 
    <%=Html.DropDownList("list")%> 

      

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

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

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

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

    使用SelectList。

    好了,关于DropDownList的用法我们今天就讨论到这里,您会用了吗?

  • 相关阅读:
    android 调试卡在:Waiting for Debugger
    将DataTable 数据插入 SQL SERVER 数据库
    android adb shell 命令大全
    GeoServer地图开发解决方案(四):发布Web地图服务(WMS)篇
    GeoServer地图开发解决方案(三):部署地图数据篇
    GeoServer地图开发解决方案(二):地图数据处理篇
    GeoServer地图开发解决方案(一):环境搭建篇
    pl/sql developer 导入sql脚本
    Myeclipse2013破解方法
    CentOS 6.4下架设NFS服务器
  • 原文地址:https://www.cnblogs.com/guanjie20/p/1901056.html
Copyright © 2011-2022 走看看