访问剃刀中枚举的原始值



在WebForm的日子里,你只需要使用ToString从枚举中获取文本描述,所有其他示例都是关于Display Attribute的,但对我来说不起作用。在.net核心中,我只想获得enum 的文本

public  class FileAttachments {
public enum FileAttachmentType {
[Display(Name = "Vessel")]
Vessel = 21,
[Display(Name = "Person Of Intrest")]
Poi =22,
[Display(Name = "Case")]
Case =23,
[Display(Name = "Passport Documents")]
Passport =25,
[Display(Name = "Certificates")]
Certificates =26,
[Display(Name = "Licenses")]
Licences =27,
[Display(Name = "Witness Statements")]
WitnessStatements =28,
[Display(Name = "Photo Evidence")]
PhotoEvidence =29        
}
public int FileUploadTypeValue  { get; set; }    
}

我正在将值存储在FileUploadTypeValue中,但当我试图访问帮助方法时,我必须转到例如模型级别的FileUploadType value将具有POI的值22,该值应打印在屏幕上给我的感兴趣的人,即POI=22 的显示名称

@foreach (var file in Model) {
<tr>
<td>
<span class="fiv-cla fiv-size-lg fiv-icon-@file.Extension"></span>
</td>
<td>
@HelperMethods.GetDisplayName(@file.FileUploadType)
@file.FileUploadType
</td>
<td>@file.CreatedDate</td>
<td>@file.File</td>
<td>@file.UploadedByUser.FirstName @file.UploadedByUser.LastName</td>

<td><a target="_blank" href="/Uploads/@file.File">View File</a></td>
</tr>
}

在我的助手类中,我有以下

public static string GetDisplayName(this Enum enumValue)
{
return enumValue.GetType()?
.GetMember(enumValue.ToString())?
.First()?
.GetCustomAttribute<DisplayAttribute>()?
.Name;
}

这行不喜欢我应该通过什么来获得字符串,所以对于22,我应该获得感兴趣的人,而不是poi

@HelperMethods.GetDisplayName(@file.FileUploadType)

这是我以前使用过的EnumHelper

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();
}
private static string lookupResource(Type resourceManagerProvider, string resourceKey)
{
foreach (PropertyInfo staticProperty in resourceManagerProvider.GetProperties(BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public))
{
if (staticProperty.PropertyType == typeof(System.Resources.ResourceManager))
{
System.Resources.ResourceManager resourceManager = (System.Resources.ResourceManager)staticProperty.GetValue(null, null);
return resourceManager.GetString(resourceKey);
}
}
return resourceKey; // Fallback with the key name
}
public static string GetDisplayValue(T value)
{
var fieldInfo = value.GetType().GetField(value.ToString());
var descriptionAttributes = fieldInfo.GetCustomAttributes(
typeof(DisplayAttribute), false) as DisplayAttribute[];
if (descriptionAttributes[0].ResourceType != null)
return lookupResource(descriptionAttributes[0].ResourceType, descriptionAttributes[0].Name);
if (descriptionAttributes == null) return string.Empty;
return (descriptionAttributes.Length > 0) ? descriptionAttributes[0].Name : value.ToString();
}
}

从枚举中获取Display NameIList<string>

EnumHelper<FileAttachmentType>.GetDisplayValues(FileAttachmentType.Vessel)

从枚举中获取Display Namestring

EnumHelper<FileAttachmentType>.GetDisplayValue(FileAttachmentType.Vessel)

最新更新