2013-03-13 118 views
0

我创建模板编辑器。如何获得枚举属性

@model dynamic 


@{ 
    var modelMetadata = Html.GetModelMetadataFor(model => model); 
    var selectList = ReflectionHelpers.GetSelectListByEnumFor(modelMetadata); 
    String name = //get property name; 
} 

与获得modelMetadata没事

但如何获取属性我不明白。

之前,我用这个代码:

但在这个时候,我不知道如何采取TEnum

我的问题是:“如何创建枚举选择列表”

+0

你的问题不是很清楚。你能否提供一个模型的例子,以及这个模板是如何被使用的? – 2013-03-13 11:49:01

+0

这是用于枚举的UIHint – Mediator 2013-03-13 11:50:52

回答

1

你可以编写一个自定义的html帮手,它将生成当前模型的下拉列表(假设该模型是枚举当然):

public static class HtmlExtensions 
{ 
    public static IHtmlString DropDownListForEnum(this HtmlHelper htmlHelper) 
    { 
     var model = htmlHelper.ViewData.Model; 
     if (model == null) 
     { 
      throw new ArgumentException("You must have a model in order to use this method"); 
     } 
     var enumType = model.GetType(); 
     if (!enumType.IsEnum) 
     { 
      throw new ArgumentException("This method works only with enum types."); 
     } 

     var fields = enumType.GetFields(
      BindingFlags.Static | BindingFlags.GetField | BindingFlags.Public 
     ); 
     var values = Enum.GetValues(enumType).OfType<object>(); 
     var items = 
      from value in values 
      from field in fields 
      let descriptionAttribute = field 
       .GetCustomAttributes(
        typeof(DescriptionAttribute), true 
       ) 
       .OfType<DescriptionAttribute>() 
       .FirstOrDefault() 
      let description = (descriptionAttribute != null) 
       ? descriptionAttribute.Description 
       : value.ToString() 
      where value.ToString() == field.Name 
      select new { Id = value, Name = description }; 

     var selectList = new SelectList(items, "Id", "Name", model); 
     return htmlHelper.DropDownList("", selectList); 
    } 
} 

,然后在模板只需拨打这个帮手:

@Html.DropDownListForEnum() 

UPDATE:

如果你想有模板的所有代码,你也能做到这一点:

@using System.ComponentModel 
@using System.Reflection 
@using System.Linq; 
@model object 

@{ 
    var model = Html.ViewData.Model; 
    if (model == null) 
    { 
     throw new ArgumentException("You must have a model in order to use this template"); 
    } 
    var enumType = model.GetType(); 
    if (!enumType.IsEnum) 
    { 
     throw new ArgumentException("This method works only with enum types."); 
    } 

    var fields = enumType.GetFields(
     BindingFlags.Static | BindingFlags.GetField | BindingFlags.Public 
    ); 
    var values = Enum.GetValues(enumType).OfType<object>(); 
    var items = 
     from value in values 
     from field in fields 
     let descriptionAttribute = field 
      .GetCustomAttributes(
       typeof(DescriptionAttribute), true 
      ) 
      .OfType<DescriptionAttribute>() 
      .FirstOrDefault() 
     let description = (descriptionAttribute != null) 
      ? descriptionAttribute.Description 
      : value.ToString() 
     where value.ToString() == field.Name 
     select new { Id = value, Name = description }; 

    var selectList = new SelectList(items, "Id", "Name", model); 
} 

@Html.DropDownList("", selectList) 
+0

是的,我也可以。但我需要分开得到。选择列表,属性名称和创建下拉列表。 – Mediator 2013-03-13 12:25:51

+0

你想在模板中做所有事情? – 2013-03-13 12:26:51

0

不幸的是我不知道如何asp.net的作品,但.NET Framework中我使用这个扩展方法:

public static IList<KeyValuePair<T, string>> ToList<T>() where T : struct 
{ 
    var type = typeof(T); 

    if (!type.IsEnum) 
    { 
     throw new ArgumentException("T must be an enum"); 
    } 

    return (IList<KeyValuePair<T, string>>) 
      Enum.GetValues(type) 
       .OfType<T>() 
       .Select(e => 
       { 
        var asEnum = (Enum)Convert.ChangeType(e, typeof(Enum)); 
        return new KeyValuePair<T, string>(e, GetEnumDescription(asEnum)); 
       }) 
       .ToArray(); 
} 

在的WinForms,我可以通过简单地调用使用它的组合框:

var pairs = EnumExtension.ToList<ContenAlignment>(); 
comboBoxFormat.DataSource = pairs; 
comboBoxFormat.ValueMember = "Key"; 
comboBoxFormat.DisplayMember = "Value"; 

也许你可以改变上面的组合框代码asp.net中您的需求。