如何获得通过MVC剃刀code枚举成员的显示名称属性? [英] How to get the Display Name Attribute of an Enum member via MVC razor code?
问题描述
我有我的模式叫推广,它的类型是所谓的UserPromotion标志枚举的属性。我枚举的成员显示属性设置如下:
[国旗]
公共枚举UserPromotion
{
无=为0x0, [显示(NAME =发送招聘信息邮寄)]
SendJobOffersByMail =为0x1, [显示(NAME =发送招聘信息通过短信)]
SendJobOffersBySms = 0X2, [显示(NAME =发送其他的东西短信)]
SendPromotionalBySms =为0x4, [显示(NAME =发送其他的东西邮寄)]
SendPromotionalByMail = 0x8中
}
现在我希望能够创造说在我看来UL认证,以显示我的推广属性选择的值。这是我迄今所做的,但问题是,我怎么能在这里得到的显示名称?
< UL>
@foreach(INT aPromotion在@ Enum.GetValues(typeof运算(UserPromotion)))
{
VAR currentPromotion =(INT)Model.JobSeeker.Promotion;
如果((currentPromotion&安培; aPromotion)== aPromotion)
{
<立GT;在这里,我不知道怎么去currentPromotion的显示属性< /李>
}
}
< / UL>
更新
首先解决的重点是从枚举越来越显示名称。低于code应该是你的问题精确解。
您可以使用此枚举辅助类:
公共静态类EnumHelper< T>
{
公共静态的IList< T>的GetValues(枚举值)
{
VAR enumValues =新的List< T>(); 的foreach(字段信息网络value.GetType()GetFields(BindingFlags.Static |。BindingFlags.Public))
{
enumValues.Add((T)Enum.Parse(value.GetType(),fi.Name,FALSE));
}
返回enumValues;
} 公共静态ŧ解析(字符串值)
{
回报(T)Enum.Parse(typeof运算(T),价值,真实);
} 公共静态的IList<串GT; GetNames(枚举值)
{
返回value.GetType()GetFields(BindingFlags.Static | BindingFlags.Public)。选择(FI => fi.Name)。.ToList();
} 公共静态的IList<串GT; GetDisplayValues(枚举值)
{
返回GetNames(值)。选择(OBJ => GetDisplayValue(解析(OBJ)))了ToList()。
} 公共静态字符串GetDisplayValue(T值)
{
。变种的FieldInfo = value.GetType()getfield命令(value.ToString()); VAR descriptionAttributes = fieldInfo.GetCustomAttributes(
typeof运算(DisplayAttribute),FALSE)作为DisplayAttribute []; 如果(descriptionAttributes == NULL)返回的String.Empty;
返回(descriptionAttributes.Length大于0)? descriptionAttributes [0] .Name点:value.ToString();
}
}
然后你可以用它在你的观点如下:
< UL>
@foreach(; UserPromotion>在@ EnumHelper&LT VAR值.GetValues(UserPromotion.None))
{
如果(价值== Model.JobSeeker.Promotion)
{
Var描述= EnumHelper< UserPromotion> .GetDisplayValue(值);
<立GT; @ Html.DisplayFor(E =>简介)LT; /李>
}
}
< / UL>
希望它帮助! :)
I've got a property in my model called "Promotion" that its type is a flag enum called "UserPromotion". Members of my enum have display attributes set as follows:
[Flags]
public enum UserPromotion
{
None = 0x0,
[Display(Name = "Send Job Offers By Mail")]
SendJobOffersByMail = 0x1,
[Display(Name = "Send Job Offers By Sms")]
SendJobOffersBySms = 0x2,
[Display(Name = "Send Other Stuff By Sms")]
SendPromotionalBySms = 0x4,
[Display(Name = "Send Other Stuff By Mail")]
SendPromotionalByMail = 0x8
}
Now I want to be able to create say a ul in my view to show the selected values of my "Promotion" property. This is what I have done so far but the problem is that how can I get the display names here?
<ul>
@foreach (int aPromotion in @Enum.GetValues(typeof(UserPromotion)))
{
var currentPromotion = (int)Model.JobSeeker.Promotion;
if ((currentPromotion & aPromotion) == aPromotion)
{
<li>Here I don't know how to get the display attribute of "currentPromotion".</li>
}
}
</ul>
UPDATE
First solution was focused on getting display names from enum. Code below should be exact solution for your problem.
You can use this helper class for enums:
public static class EnumHelper<T>
{
public static IList<T> GetValues(Enum value)
{
var enumValues = new List<T>();
foreach (FieldInfo fi in value.GetType().GetFields(BindingFlags.Static | BindingFlags.Public))
{
enumValues.Add((T)Enum.Parse(value.GetType(), fi.Name, false));
}
return enumValues;
}
public static T Parse(string value)
{
return (T)Enum.Parse(typeof(T), value, true);
}
public static IList<string> GetNames(Enum value)
{
return value.GetType().GetFields(BindingFlags.Static | BindingFlags.Public).Select(fi => fi.Name).ToList();
}
public static IList<string> GetDisplayValues(Enum value)
{
return GetNames(value).Select(obj => GetDisplayValue(Parse(obj))).ToList();
}
public static string GetDisplayValue(T value)
{
var fieldInfo = value.GetType().GetField(value.ToString());
var descriptionAttributes = fieldInfo.GetCustomAttributes(
typeof(DisplayAttribute), false) as DisplayAttribute[];
if (descriptionAttributes == null) return string.Empty;
return (descriptionAttributes.Length > 0) ? descriptionAttributes[0].Name : value.ToString();
}
}
And then you can use it in your view as following:
<ul>
@foreach (var value in @EnumHelper<UserPromotion>.GetValues(UserPromotion.None))
{
if (value == Model.JobSeeker.Promotion)
{
var description = EnumHelper<UserPromotion>.GetDisplayValue(value);
<li>@Html.DisplayFor(e => description )</li>
}
}
</ul>
Hope it helps! :)
这篇关于如何获得通过MVC剃刀code枚举成员的显示名称属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!