public static MvcHtmlString EnumToDropDownList(this HtmlHelper helper, Enum eEnum,string name) { var selectList = new List<SelectListItem>(); var enumType = eEnum.GetType(); foreach (var value in Enum.GetValues(enumType)) { var field = enumType.GetField(value.ToString()); var option = new SelectListItem() { Value = value.ToString() }; var display = field.GetCustomAttributes(typeof(DisplayAttribute), false).FirstOrDefault() as DisplayAttribute; option.Text = display != null ? display.Name : value.ToString(); option.Selected = Equals(value, eEnum); selectList.Add(option); } return helper.DropDownList(name, selectList); }
先通过Enum.GetValues方法得到枚举类型的各个值,然后通过反射得到DisplayAttribute特性。然后将获取到name作为下拉框option的Text。调用:
@Html.EnumToDropDownList(Model.QuestionType, )
EnumToDropDownListFor实现起来就简单啦,关键是找到类型。
public static MvcHtmlString EnumToDropDownListFor<TModel, TProperty>(this HtmlHelper<TModel> html, Expression<Func<TModel, TProperty>> expression, object htmlAttributes=null) { ModelMetadata modelMetadata = ModelMetadata.FromLambdaExpression(expression, html.ViewData); var htmlAttributes2 = AnonymousObjectToHtmlAttributes(htmlAttributes); var enumType = modelMetadata.ModelType; var selectList = new List<SelectListItem>(); foreach (var value in Enum.GetValues(enumType)) { var field = enumType.GetField(value.ToString()); var option = new SelectListItem() { Value = value.ToString() }; var display = field.GetCustomAttributes(typeof(DisplayAttribute), false).FirstOrDefault() as DisplayAttribute; option.Text = display != null ? display.Name : value.ToString(); option.Selected = Equals(value, modelMetadata.Model); selectList.Add(option); } return html.DropDownList(modelMetadata.PropertyName, selectList,htmlAttributes2);
调用更加简单:
@Html.EnumToDropDownListFor(model => model.QuestionType)
结果一样,且可以扩展样式,匹配选中。
helper代码
HtmlHelpers { public static MvcHtmlString Image(this HtmlHelper helper, string url, int length) { ); tagA.MergeAttribute(, url); tagA.MergeAttribute(, ); ); img.MergeAttribute(, url); img.MergeAttribute(, , length)); tagA.InnerHtml = img.ToString(); MvcHtmlString.Create(tagA.ToString()); } public static MvcHtmlString EnumToDropDownList(this HtmlHelper helper, Enum eEnum,string name) { var selectList = new List<SelectListItem>(); var enumType = eEnum.GetType(); foreach (var value in Enum.GetValues(enumType)) { var field = enumType.GetField(value.ToString()); var option = new SelectListItem() { Value = value.ToString() }; var display = field.GetCustomAttributes(typeof(DisplayAttribute), false).FirstOrDefault() as DisplayAttribute; option.Text = display != null ? display.Name : value.ToString(); option.Selected = Equals(value, eEnum); selectList.Add(option); } return helper.DropDownList(name, selectList); } public static MvcHtmlString EnumToDropDownListFor<TModel, TProperty>(this HtmlHelper<TModel> html, Expression<Func<TModel, TProperty>> expression, object htmlAttributes=null) { ModelMetadata modelMetadata = ModelMetadata.FromLambdaExpression(expression, html.ViewData); var htmlAttributes2 = AnonymousObjectToHtmlAttributes(htmlAttributes); var enumType = modelMetadata.ModelType; var selectList = new List<SelectListItem>(); foreach (var value in Enum.GetValues(enumType)) { var field = enumType.GetField(value.ToString()); var option = new SelectListItem() { Value = value.ToString() }; var display = field.GetCustomAttributes(typeof(DisplayAttribute), false).FirstOrDefault() as DisplayAttribute; option.Text = display != null ? display.Name : value.ToString(); option.Selected = Equals(value, modelMetadata.Model); selectList.Add(option); } return html.DropDownList(modelMetadata.PropertyName, selectList,htmlAttributes2); } public static MvcHtmlString A(this HtmlHelper helper, string text, string url, int id) { ); tagA.MergeAttribute(, url); tagA.MergeAttribute(, id.ToString()); tagA.InnerHtml = text; return MvcHtmlString.Create(tagA.ToString()); } internal static MvcHtmlString ImageHelper(HtmlHelper html, ModelMetadata metadata, IDictionary<string, object> htmlAttributes = null) { value = metadata.Model.ToString(); if (string.IsNullOrEmpty(value)) { return MvcHtmlString.Empty; } ); img.Attributes.Add(, value); //属性名 img.Attributes.Add(, metadata.PropertyName); img.MergeAttributes(htmlAttributes, true); ); tagA.MergeAttribute(,value); tagA.MergeAttribute(, ); tagA.InnerHtml = img.ToString(); return MvcHtmlString.Create(tagA.ToString()); } public static MvcHtmlString ImageFor<TModel, TProperty>(this HtmlHelper<TModel> html, Expression<Func<TModel, TProperty>> expression, object htmlAttributes) { ModelMetadata modelMetadata = ModelMetadata.FromLambdaExpression(expression, html.ViewData); htmlAttributes2 = AnonymousObjectToHtmlAttributes(htmlAttributes); return ImageHelper(html, modelMetadata , htmlAttributes2); } public static MvcHtmlString ImageFor<TModel, TProperty>(this HtmlHelper<TModel> html, Expression<Func<TModel, TProperty>> expression) { return ImageFor(html, expression, null); } private static RouteValueDictionary AnonymousObjectToHtmlAttributes(object htmlAttributes) { RouteValueDictionary routeValueDictionary = new RouteValueDictionary(); if (htmlAttributes != null) { foreach (PropertyDescriptor propertyDescriptor in TypeDescriptor.GetProperties(htmlAttributes)) { routeValueDictionary.Add(propertyDescriptor.Name.Replace(, ), propertyDescriptor.GetValue(htmlAttributes)); } } return routeValueDictionary; } } public class ImageModelMetadataProvider : DataAnnotationsModelMetadataProvider { protected override ModelMetadata CreateMetadata(IEnumerable<Attribute> attributes, Type containerType, Func<object> modelAccessor, Type modelType, string propertyName) { var meta= base.CreateMetadata(attributes, containerType, modelAccessor, modelType, propertyName); if (meta.DataTypeName==DataType.ImageUrl.ToString() && string.IsNullOrEmpty(meta.TemplateHint)) { meta.TemplateHint = ; } return meta; } }
View Code代码已更新到:https://github.com/stoneniqiu/Portal.MVC