Razor视图中DropDownList数据绑定的简化

这只是自己写的一个class,用来简化绑定ViewModel属性的方式. enjoy it.

  

实际使用:

简单类型下拉框,String和不包括枚举的各基元类型

 

1 @Html.ComboxListFor(m => m.Month,new []{1,2,3,4,5,6,7,8,9,10,11,12}, "--月--"new { @class = "someStyle" })

 

枚举下拉框

 

1 @Html.ComboxEnumListFor(m => m.IDType, typeof(IDType), "--请选择证件类型--")

 

稍特殊类型下拉框,下面例子只是其中一种方式,不列举了

 

1 @Html.ComboxListFor(x => x.YourCity, CityBLL.GetList(), "CityName""CityID""--请选择城市--"new { @class = "someStyle" })

 

抛具体依赖的代码 

 

  1 using System.Collections.Generic;
  2 using System.Linq;
  3 using System.Linq.Expressions;
  4 using System.Web.Mvc.Html;
  5 
  6 namespace System.Web.Mvc
  7 {
  8     /// <summary>
  9     /// Extended the HtmlHelper for DropDownList
 10     /// </summary>
 11     public  static class ComboxExtensions
 12     {
 13 
 14         #region 简单类型下拉框
 15 
 16         public static MvcHtmlString ComboxListFor<TModel, TProperty, TPropertyType>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<TPropertyType> values)
 17         {
 18             return ComboxListFor(htmlHelper, expression, values, new object());
 19         }
 20 
 21         public static MvcHtmlString ComboxListFor<TModel, TProperty, TPropertyType>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<TPropertyType> values, object htmlAttributes)
 22         {
 23             return ComboxListFor(htmlHelper, expression, values, nullnew object());
 24         }
 25 
 26         public static MvcHtmlString ComboxListFor<TModel, TProperty, TPropertyType>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<TPropertyType> values, string optionLabel)
 27         {
 28             return ComboxListFor(htmlHelper, expression, values, optionLabel, new object());
 29         }
 30 
 31         public static MvcHtmlString ComboxListFor<TModel, TProperty, TPropertyType>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<TPropertyType> values, string optionLabel, object htmlAttributes)
 32         {
 33             return ComboxListForIt(htmlHelper, expression, values, nullnull, optionLabel, htmlAttributes);
 34         }
 35 
 36         #endregion
 37 
 38         #region 枚举下拉框
 39 
 40         public static MvcHtmlString ComboxEnumListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, Type enumType)
 41         {
 42             return ComboxEnumListFor(htmlHelper, expression, enumType, new object());
 43         }
 44 
 45         public static MvcHtmlString ComboxEnumListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, Type enumType, object htmlAttributes)
 46         {
 47             return ComboxEnumListFor(htmlHelper, expression, enumType, nullnew object());
 48         }
 49 
 50         public static MvcHtmlString ComboxEnumListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, Type enumType, string optionLabel)
 51         {
 52             return ComboxEnumListFor(htmlHelper, expression, enumType, optionLabel, new object());
 53         }
 54 
 55         public static MvcHtmlString ComboxEnumListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, Type enumType, string optionLabel, object htmlAttributes)
 56         {
 57             if (!enumType.IsEnum)
 58             {
 59                 throw new ArgumentException("参数必须为枚举类型");
 60             }
 61 
 62             var enumItems1 = (from Enum e in Enum.GetValues(enumType)
 63                              select new KeyValuePair<Enum,string>(e,EnumUtils.GetDescription(e))
 64                              ).ToList();
 65 
 66             return ComboxListForIt(htmlHelper, expression, enumItems1, "Key""Value", optionLabel, htmlAttributes);
 67         }
 68 
 69         #endregion
 70 
 71         #region 稍特殊类型下拉框,ViewModel属性对应的待选项是个另外一个对象的属性集合
 72 
 73         public static MvcHtmlString ComboxListFor<TModel, TProperty, TPropertyType>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<TPropertyType> values, string dataValueField, string dataTextField)
 74         {
 75             return ComboxListFor(htmlHelper, expression, values,dataValueField, dataTextField, new object());
 76         }
 77 
 78         public static MvcHtmlString ComboxListFor<TModel, TProperty, TPropertyType>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<TPropertyType> values,string dataValueField, string dataTextField, object htmlAttributes)
 79         {
 80             return ComboxListFor(htmlHelper, expression, values,dataValueField, dataTextField, nullnew object());
 81         }
 82 
 83         public static MvcHtmlString ComboxListFor<TModel, TProperty, TPropertyType>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<TPropertyType> values,string dataValueField, string dataTextField, string optionLabel)
 84         {
 85             return ComboxListFor(htmlHelper, expression, values, dataValueField, dataTextField, optionLabel, new object());
 86         }
 87 
 88         public static MvcHtmlString ComboxListFor<TModel, TProperty,TPropertyType>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<TPropertyType> values, string dataValueField, string dataTextField, string optionLabel, object htmlAttributes)
 89         {
 90             return ComboxListForIt(htmlHelper, expression, values, dataValueField, dataTextField, optionLabel, htmlAttributes);
 91         }
 92 
 93         #endregion
 94 
 95         #region base,其实只有这个
 96 
 97         private static MvcHtmlString ComboxListForIt<TModel, TProperty, TPropertyType>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<TPropertyType> values, string dataValueField, string dataTextField, string optionLabel, object htmlAttributes)
 98         {
 99             Type tPropertyType = typeof(TPropertyType);
100 
101             var modelMetadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
102             var selectValue = new object();
103 
104             if (modelMetadata.Model != null)
105             {
106                 selectValue = modelMetadata.Model;
107             }
108 
109             SelectList selectList;
110 
111             if (tPropertyType == typeof(int)
112                 || tPropertyType == typeof(uint)
113                 || tPropertyType == typeof(long)
114                 || tPropertyType == typeof(ulong)
115                 || tPropertyType == typeof(long)
116                 || tPropertyType == typeof(ulong)
117                 || tPropertyType == typeof(short)
118                 || tPropertyType == typeof(ushort)
119                 || tPropertyType == typeof(byte)
120                 || tPropertyType == typeof(sbyte)
121                 || tPropertyType == typeof(decimal)
122                 )
123             {
124                 var values1 = values.Select(x => new CBItem(x));
125                 selectList = new SelectList(values1, "Value","Text",selectValue);
126             }
127             else
128             {
129 
130                 if (tPropertyType.IsPrimitive || tPropertyType == typeof (string))
131                 {
132                     selectList = new SelectList(values, selectValue);
133                 }
134                 else
135                 {
136                     if (dataTextField != null && dataValueField != null)
137                     {
138                         selectList = new SelectList(values, dataValueField, dataTextField, selectValue);
139                     }
140                     else
141                     {
142                         selectList = new SelectList(values, selectValue);
143                     }
144                 }
145 
146             }
147 
148             return htmlAttributes == null
149                        ? (optionLabel == null
150                               ? htmlHelper.DropDownListFor(expression, selectList)
151                               : htmlHelper.DropDownListFor(expression, selectList, optionLabel))
152                        : (optionLabel == null
153                               ? htmlHelper.DropDownListFor(expression, selectList, htmlAttributes)
154                               : htmlHelper.DropDownListFor(expression, selectList, optionLabel, htmlAttributes));
155         }
156 
157         /// <summary>
158         /// 一个辅助的待选项类
159         /// </summary>
160          public class CBItem  
161         {
162 
163             private string _text = String.Empty;
164             private object _value = default(object);
165 
166             public CBItem()
167             { }
168 
169             public CBItem(object value, string text)
170             {
171                 _text = text;
172                 _value = value;
173             }
174 
175             public CBItem(object value)
176             {
177                 _text = value.ToString();
178                 _value = value;
179             }
180 
181             /// <summary>
182             /// Text
183             /// </summary>
184             public string Text
185             {
186                 get { return _text; }
187                 set { _text = value; }
188             }
189 
190             /// <summary>
191             /// Value
192             /// </summary>
193             public object Value
194             {
195                 get { return _value; }
196                 set { _value = value; }
197             }
198 
199         }
200 
201         #endregion
202 
203     }
204 }

 

 

 

 

posted @ 2012-05-02 11:55  Leo Lee  阅读(2469)  评论(0编辑  收藏  举报