我的模型中有一个名为"Promotion"的属性,它的类型是一个名为"UserPromotion"的标志枚举.我的枚举成员的显示属性设置如下:
[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
}
Run Code Online (Sandbox Code Playgroud)
现在我希望能够在我的视图中创建一个ul来显示我的"Promotion"属性的选定值.这是我到目前为止所做的,但问题是如何在这里获取显示名称?
<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 …Run Code Online (Sandbox Code Playgroud) 我有一个组合框,我在其中显示一些条目,如:
Equals
Not Equals
Less Than
Greater Than
Run Code Online (Sandbox Code Playgroud)
请注意,这些字符串包含空格.我有一个定义的枚举,它匹配这些条目,如:
enum Operation{Equals, Not_Equals, Less_Than, Greater_Than};
Run Code Online (Sandbox Code Playgroud)
由于不允许空间,我使用了_字符.
现在,有没有办法将给定的字符串自动转换为枚举元素而无需在C#中编写循环或一组if条件?