0

我想为我的ASP.NET MVC3网站编写一个帮助器,它将能够返回一个新的SelectList,其中包含一个枚举的所有Description属性标记在ASP.NET MVC3中使用显示标记填充DropDownListFor

例如,具有以下枚举:

public enum Test 
{ 
    [Display(Name = "Membre 1")] 
    Member1, 

    [Display(Name = "Membre 2")] 
    Member2 
} 

我希望能够填补DropDownListFor的东西,如:

@Html.DropDownListFor(m => m.MyTest, MyHelper(Test)) 

(与MyTestTest变量)。

,我希望我的DropDownList包含:

  • Membre 1
  • Membre 2
  • 我用这个工作助手:

    public static string GetEnumDescription(this Enum value) 
    { 
        Type enumType = value.GetType(); 
        var enumValue = Enum.GetName(enumType, value); 
        MemberInfo member = enumType.GetMember(enumValue)[0]; 
    
        var attrs = member.GetCustomAttributes(typeof(DisplayAttribute), false); 
        var outString = ((DisplayAttribute)attrs[0]).Name; 
    
        if (((DisplayAttribute)attrs[0]).ResourceType != null) 
        { 
         outString = ((DisplayAttribute)attrs[0]).GetName(); 
        } 
    
        return outString; 
    } 
    

    ...但我不能让它工作在SelectList

    我该如何修改thi s直接“合并”它直接在我的@Html.DropDownListFor帮手?

    我在互联网上看到过一些帮手,特别是herehere,但没有人为我工作。有没有人能够共享一个优雅帮手,它返回一个枚举成员的所有Display属性,以便将它们放在一个DropDownListFor

    回答

    1

    以下是我使用的。这是我在网上发现的一个稍微修改过的版本。我会给予信贷,信贷到期,但我不记得在那里我发现它原来在这一点上:

    public static SelectList ToSelectList(this Enum enumeration) 
    { 
        var list = (from Enum d in Enum.GetValues(enumeration.GetType()) 
           select new { Value = Enum.GetName(enumeration.GetType(), d), Text = d.GetDescription() }).ToList(); 
    
        var selectedValue = (int)Enum.Parse(enumeration.GetType(), Enum.GetName(enumeration.GetType(), enumeration)); 
    
        return new SelectList(list, "Value", "Text"); 
    } 
    
    public static string GetDescription(this Enum en) 
    { 
        Type type = en.GetType(); 
        System.Reflection.MemberInfo[] memInfo = type.GetMember(en.ToString()); 
    
        if (memInfo != null && memInfo.Length > 0) 
        { 
         object[] attrs = memInfo[0].GetCustomAttributes(typeof(System.ComponentModel.DataAnnotations.DisplayAttribute), false); 
    
         if (attrs != null && attrs.Length > 0) 
          return ((System.ComponentModel.DataAnnotations.DisplayAttribute)attrs[0]).GetName(); 
        } 
    
        return en.ToString(); 
    } 
    

    在你看来,你会使用它:

    @Html.DropDownListFor(m => m.MyEnumProperty, Model.MyEnumProperty.ToSelectList()) 
    
    +0

    我只是尝试你的答案,因为它是第一个。我在Tools \ Helpers \ AttributesHelperExtensions.cs中创建了两个方法,这是一个静态类,我写了一些帮助器。在我看来,'ToSelectList()'不能识别...有没有使用或我忘记的其他东西? – AlexB

    +0

    对不起。您还需要在视图顶部指向扩展名的“@ using”指令。就像在C#代码中的其他任何地方一样,您必须让编译器知道在哪里找到该方法。 –

    1

    为了实现枚举类型的数据,我认为最简单的方法是使用自定义的Enum helper和Templates。以下是我如何在我的项目中实施它们。

    1)创建枚举助手

    public static class EnumHelper 
        { 
         public static IEnumerable<SelectListItem> GetItems(this Type enumType, int? selectedValue) 
         { 
          if (!typeof (Enum).IsAssignableFrom(enumType)) 
          { 
           throw new ArgumentException("Type must be an enum"); 
          } 
          string[] names = Enum.GetNames(enumType); 
          IEnumerable<int> values = Enum.GetValues(enumType).Cast<int>(); 
    
          IEnumerable<SelectListItem> items = names.Zip(values, (name, value) => 
           new SelectListItem 
           { 
            Text = GetName(enumType, name), 
            Value = value.ToString(), 
            Selected = value == selectedValue 
           } 
           ); 
          return items; 
         } 
         // Get Display Name 
         private static string GetName(Type enumType, string name) 
         { 
          string result = name; 
          DisplayAttribute attribute = enumType.GetField(name) 
           .GetCustomAttributes(false) 
           .OfType<DisplayAttribute>() 
           .FirstOrDefault(); 
          if (attribute != null) 
          { 
           result = attribute.GetName(); 
          } 
          return result; 
         } 
    
         public static string GetItemName(this Type enumType, int selectedValue) 
         { 
          if (!typeof (Enum).IsAssignableFrom(enumType)) 
          { 
           throw new ArgumentException("Type must be an enum"); 
          } 
          var itemName = GetName(enumType, Enum.GetNames(enumType)[selectedValue]); 
          return itemName; 
         } 
        } 
    

    2)创建共享文件夹中的文件夹中呼叫 “DisplayTemplates”。

    3)在“DisplayTemmplates”里面创建视图。该视图如下所示:

    @using Demo.Web.Helper 
    @{ 
        var itemName = typeof(Test).GetItemName((int)Model); 
    } 
    

    4)在共享文件夹中创建floder调用“EditorTemplates”。

    5)在“EditorTemplates”中创建视图。该视图如下所示:

    @using Demo.Web.Helper 
    @{ 
        var items = typeof (Test).GetItems((int?)Model); 
    }  
    @Html.DropDownList("",items) 
    

    在这里,您已完成所有助手和模板,可供使用。当你想实现枚举类型的数据,只是用它象下面这样:

    型号

    public class MyModel 
    { 
        public int Id { get; set; } 
        // 
        public Test Test { get; set; } 
    } 
    

    查看

    @Html.DisplayFor(m => m.Test) 
    or 
    @Html.EditorFor(m => m.Test) 
    

    希望它能帮助。

    +0

    这是我还在寻找什么。 – 2013-11-27 20:34:39

    +0

    对不起,迟到的答案。我非常喜欢你的解决方案,但是我已经实现了克里斯的解决方案,它与我所做的最接近,并且需要更少的修改来放置我自己的项目。但是,我upvoted你的答案;-)再次感谢! – AlexB