MVC 绑定DropDownList 分层次显示

  • 先说一下SelectList类:  表示一个列表

                   构造函数1:SelectList(IEnumerable)使用列表的指定项来初始化SelectList 类的新实例。

                   构造函数2:SelectList(IEnumerable, Object)  使用列表的指定项和选定的值来初始化SelectList 类的新实例。

                   构造函数3:SelectList(IEnumerable, String, String)使用列表的指定项、数据值字段和数据文本字段来初始化SelectList 类的新实例。

                   构造函数4:SelectList(IEnumerable, String, String, Object)使用列表的指定项、数据值字段、数据文本字段和选定的值来初始化新实例。



  • 使用方法1:直接写在html中的

[html]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. @Html.DropDownListFor(m => m.proType, new[]{new SelectListItem(){Text="--请选择--",Value=""},new SelectListItem(){Text="1",Value="1"}}, new{ @id="ddl",@class="cssClass"})  

  • 使用方法2:动态添加的

          (1):获取列表项

[csharp]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. public static SelectList GetSelectList()  
  2. {  
  3.     List<SelectListItem> l = new List<SelectListItem>();  
  4.     for (int i = 0; i < 10; i++)  
  5.     {  
  6.         SelectListItem sli = new SelectListItem() { Text = i.ToString(), Value = i.ToString() };  
  7.         l.Add(sli);  
  8.     }  
  9.     SelectList sl = new SelectList(l, "Value""Text");  
  10.     return sl;  
  11.   
  12. }  
          (2)把列表项传递过去

[csharp]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. ViewBag.ddl = GetSelectList();  
          (3)在视图中,把列表项遍历出来

[html]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. @Html.DropDownListFor(m => m.proType, ViewBag.ddl as SelectList, new{ @id="ddl",@class="cssClass"})  

  • 无限级分类展示:

            如果有一个类别表,是无限级分类的 我们假设  表名: Type   列: id(主键)     name(名称)    fid(父ID).       如果fid为0或null,说明没有子项.我们如何使用

Html.DropDownListFor(...)把它分层次的展示出来.形如下图:



          第一步:获取数据

[csharp]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. /// <summary>  
  2. /// 获取要显示的内容  
  3. /// </summary>  
  4. /// <param name="fid">父ID</param>  
  5. /// <returns>List<Type> 类别对列表</returns>  
  6. public List<Type> GetClassList(int fid = 0)  
  7. {  
  8.     List<Type> l = new List<Type>();  
  9.     DCDataContext dc = new DCDataContext();  
  10.     var rs = dc.Type.Where(r => r.fId == fid);  
  11.     if (rs.Count() > 0)  
  12.     {  
  13.         int deep = 1;  
  14.         foreach (Type t in rs)  
  15.         {  
  16.             //获取深度  
  17.             deep = GetDeep(t.id);  
  18.             //拼接显示的形式  
  19.             string line = "├("+deep+")";  
  20.             string space ="";  
  21.             for (int i = 1; i < deep; i++)  
  22.             {  
  23.                 space += HttpUtility.HtmlDecode("   ");  
  24.             }  
  25.             t.name = space + line + t.name;    
  26.             //把查询的对象添加到列表中  
  27.             l.Add(t);  
  28.             l.AddRange(GetClassList(t.id));  
  29.             //把深度值初始化  
  30.             deep = 1;  
  31.         }  
  32.     }  
  33.     return l;   
  34. }  
  35.   
  36.   
  37. /// <summary>  
  38. /// 获取指定对象的深度  
  39. /// </summary>  
  40. /// <param name="id">对象ID</param>  
  41. /// <returns>深度</returns>  
  42. private int GetDeep(int id)  
  43. {  
  44.     int deep=1;  
  45.     DCDataContext dc = new DCDataContext();  
  46.     Type t = dc.Type.Where(r => r.id == id).FirstOrDefault();  
  47.     if (t != null)  
  48.     {  
  49.         if (t.fId == null || t.fId == 0)  
  50.         {  
  51.             return deep;  
  52.         }  
  53.         else  
  54.         {  
  55.             deep+=GetDeep((int)t.fId);  
  56.         }  
  57.     }  
  58.     else  
  59.     {  
  60.         return deep;  
  61.   
  62.     }  
  63.     return deep;  
  64. }  

          第二步:调用数据

[csharp]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. public ActionResult Index()  
  2. {  
  3.     ViewBag.classList = GetClassList(0);  
  4.     return View();  
  5. }  


          第三部:视图中显示

[html]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. @Html.DropDownListFor(m => m.id, new SelectList(@ViewBag.ClassList, "Id", "Name"), new { })  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值