0

In my C# web API, I have defined an enum with a specified display name:

public enum Countries
{
    Australia,
    [Display(Name="New Zealand")] NewZealand
}

I send this list as a list of strings to my Angular project to display it in a dropdown menu, but I still got "NewZealand" displayed. How can I send the right data to my Angular Project without having to create a new list or enum list in Angular (to avoid duplicate data and thus problems when values would change)?

I also tried with the following code with the same outcome:

public enum Countries
{
    Australia,
    [Description("New Zealand")] NewZealand
}

EDIT: here's the code that converts my enums to a list of string:

        public List<string> GetAllCountries()
        {
            var listOfAllCountries = new List<string>();
            foreach (var country in Enum.GetValues(typeof(Countries)))
            {
                listOfAllCountries.Add(country.ToString());
            }
            return listOfAllCountries ;
        }
Amaryllis
  • 15
  • 6
  • Please show the code that creates your list of strings. – Fildor May 25 '21 at 09:41
  • Does this answer your question? [How to get the Display Name Attribute of an Enum member via MVC Razor code?](https://stackoverflow.com/questions/13099834/how-to-get-the-display-name-attribute-of-an-enum-member-via-mvc-razor-code) – Fildor May 25 '21 at 09:52
  • Have a look into this: https://dotnetfiddle.net/elkNT7 - It's a slight modification of an answer from the marked dupe. – Fildor May 25 '21 at 10:02
  • I went through the first answer of Fildor and tried some suggestions out, but that doesn't seem to work. I'm not able to access certain methods like GetDisplayName for one reason or another. – Amaryllis May 25 '21 at 11:00
  • Just [edit] into the question what you tried and what the errors were. We can go from there. – Fildor May 25 '21 at 11:04
  • The second answer did the trick. Thank you very much! How can I upvote your answer? – Amaryllis May 25 '21 at 11:08
  • The one from the Fiddle? – Fildor May 25 '21 at 11:09
  • Yep, the one from the fiddle :-) – Amaryllis May 25 '21 at 11:29

1 Answers1

0

Slight variation of an answer from this answer:

using System;
using System.Linq;
using System.Reflection;
using System.ComponentModel.DataAnnotations;
                    
public class Program
{
    public static void Main()
    {
        var names = GetNames();
        foreach( var name in names )
            Console.WriteLine(name);
        
    }

    public static List<string> GetNames()
    {
         // Convert all values of the enum to their display names
         return Enum.GetValues<Countries>().Select(v => v.GetDisplayName()).ToList();
    }
}

public enum Countries
{
    Australia,
    [Display(Name="New Zealand")] 
    NewZealand,
}

public static class CountriesExtensions
{
    public static string GetDisplayName(this Countries enumValue)
    {
        // If it has a DisplayAttribute, get the name from it:
        string name = enumValue.GetType()
                        .GetMember(enumValue.ToString())
                        .First()
                        .GetCustomAttribute<DisplayAttribute>()
                        ?.GetName(); // Null-Check!
         // No Attribute: Fall back to default.
         name = name ?? enumValue.ToString();
    }
}

In Action: Fiddle

Fildor
  • 14,510
  • 4
  • 35
  • 67