-1

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

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

@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

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

@HelperMethods.GetDisplayName(@file.FileUploadType)
c-sharp-and-swiftui-devni
  • 3,743
  • 4
  • 39
  • 100

1 Answers1

1

Here's a EnumHelper I used before.

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

EnumHelper<FileAttachmentType>.GetDisplayValues(FileAttachmentType.Vessel)

Get string of Display Name from Enum

EnumHelper<FileAttachmentType>.GetDisplayValue(FileAttachmentType.Vessel)
Michael Wang
  • 3,782
  • 1
  • 5
  • 15
  • EnumHelper I posted worked for me. Due to confusing about the relationship between FileUploadType an FileUploadTypeValue, I didn't provide test for your models. You could try it by yourself and if you need further help, please detail your models. – Michael Wang Jul 30 '20 at 09:41
  • Sorry what should FileAttachmentType the main model is called FileAttachmentVM should that be there. – c-sharp-and-swiftui-devni Jul 31 '20 at 21:57
  • Do use this @EnumHelper.GetDisplayValues(FileAttachmentType.Vessel) its not reconizing EnumHelper even though i added it using stament and the class to project – c-sharp-and-swiftui-devni Jul 31 '20 at 21:59