locked
Enum Display within View RRS feed

  • Question

  • User-1026607185 posted

    HI all, 

    I have a Enum like below, 

    public enum CarTypes
    {
    [Display(Name = "Ford Car ")]
    Ford = 1,
    [Display(Name = "Audi Car")]
    Audi = 2
    }

    my model as below 

    Public Class Car

    {

    public int CarTypeId { get; set; }

    }

    If my model returns list of 

     var cars = new List<Car>();

    cars.Add(new Car(){CarTypeId = 1});

    cars.Add(new Car(){CarTypeId = 2});

    and my mvc view should dispy (only show display name when my value equal to my enum type id value ) like  below : 

    -----

    Ford Car

    Audi Car

    ----

    how to do this with mvc Razor view ?

    Wednesday, September 25, 2019 4:25 PM

Answers

  • User-17257777 posted

    Hi sujee_fdo,

    You can make an extension method to achieve it.

    Demo

    Model:

    namespace Test6.Models
    {
        public class Car
        {
            public int CarTypeid { get; set; }
            public CarTypes CarType { get; set; }
        }
    
        public enum CarTypes
        {
            [Display(Name = "Ford Car ")]
            Ford = 1,
            [Display(Name = "Audi Car")]
            Audi = 2 
        }
    
        public static class EnumExtensions
        {
            public static string GetDisplayName(this Enum enumValue)
            {
                return enumValue.GetType()
                                .GetMember(enumValue.ToString())
                                .First()
                                .GetCustomAttribute<DisplayAttribute>()
                                .GetName();
           }
        }
    }
    

    View:

    @model IEnumerable<Test6.Models.Car>
    @using Test6.Models                    @* don’t forget to add this using statement *@
    
    
    @foreach(var car in Model)
    {
        @car.CarType.GetDisplayName();
        <br />
    }
    

    Test Result:

    Best Regards,

    Jiadong Meng

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 26, 2019 3:16 AM

All replies

  • User-17257777 posted

    Hi sujee_fdo,

    You can make an extension method to achieve it.

    Demo

    Model:

    namespace Test6.Models
    {
        public class Car
        {
            public int CarTypeid { get; set; }
            public CarTypes CarType { get; set; }
        }
    
        public enum CarTypes
        {
            [Display(Name = "Ford Car ")]
            Ford = 1,
            [Display(Name = "Audi Car")]
            Audi = 2 
        }
    
        public static class EnumExtensions
        {
            public static string GetDisplayName(this Enum enumValue)
            {
                return enumValue.GetType()
                                .GetMember(enumValue.ToString())
                                .First()
                                .GetCustomAttribute<DisplayAttribute>()
                                .GetName();
           }
        }
    }
    

    View:

    @model IEnumerable<Test6.Models.Car>
    @using Test6.Models                    @* don’t forget to add this using statement *@
    
    
    @foreach(var car in Model)
    {
        @car.CarType.GetDisplayName();
        <br />
    }
    

    Test Result:

    Best Regards,

    Jiadong Meng

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 26, 2019 3:16 AM
  • User-1026607185 posted

    Hi, 

    thanks for the reply, i managed to get it work witb your code sample  

    cool

    thanks

    Thursday, September 26, 2019 9:09 AM