简体   繁体   English

访问 razor 中枚举的原始值

[英]Accessing raw value of an enum in razor

In WebForm days you would have just used ToString to get the text description from the enum all the other examples are about the Display Attribute but its not working for me.In .net core all i want to get is the text of enum在 WebForm 时代,您只需要使用 ToString 从枚举中获取文本描述,所有其他示例都是关于显示属性的,但它对我不起作用。在 .net 核心中,我想要得到的只是枚举的文本

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; }    
}

I am Storing the value in FileUploadTypeValue but when I try to access the help method I have to go for example in the model level FileUploadTypeValue will have the value 22 for POI which should print out on screen for me Person Of Interest which is the Display Name of Poi=22我将值存储在 FileUploadTypeValue 但是当我尝试访问帮助方法时,我必须 go 例如在 model 级别 FileUploadTypeValue 将具有 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>
}

In MY Helper Class I have the following在我的助手 Class 我有以下

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

Its this line its not liking what should i be passing to get the string so for 22 I should get Person Of Interest instead of poi它的这条线不喜欢我应该传递什么来获取字符串,所以对于 22 我应该得到感兴趣的人而不是 poi

@HelperMethods.GetDisplayName(@file.FileUploadType)

Here's a EnumHelper I used before.这是我以前使用的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();
    }
}

Get IList<string> of Display Name from Enum从枚举中获取Display NameIList<string>

EnumHelper<FileAttachmentType>.GetDisplayValues(FileAttachmentType.Vessel)

Get string of Display Name from Enum从枚举中获取Display Name string

EnumHelper<FileAttachmentType>.GetDisplayValue(FileAttachmentType.Vessel)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM