Enum names instead of int values in ASP.NET Core web app with Razor pages

I need to output in a Grid column the name of enum values instead of the int values themselves. I found a couple of forum posts regarding this same thing but they are all dealing with MVC and version 1 of EJ, not EJ2. Please detail how I can (easily) bind my data, using either a data manager or a List, and have the Grid column display the enum name.

Again, this is for ASP.NET Core web app, NOT MVC, using Razor pages.

Thanks

6 Replies

JI JimN July 3, 2018 09:41 AM UTC

I have solved this, and quite easily. All I needed to do was to add the Newtonsoft.Json package to my ASP.NET Core 2.1 web app project and then add a JsonConverter attribute to the Model property that I wanted to output using the name of an enum instead of the value. 

In my model class I have a property defined as:

    [JsonConverter(typeof(StringEnumConverter))]
    [DisplayName("Status")]
    public ItemStatusType ItemStatus { get; set; } = ItemStatusType.New;

where ItemStatusType is defined as:

    public enum ItemStatusType
    {
/// <summary>
/// Used for newly created items.
/// </summary>
[EnumMember(Value = "New")]
New,
/// <summary>
/// Used when work has started on an item.
/// </summary>
[EnumMember(Value = "InProgress")]
    InProgress,
/// <summary>
/// Used when an item is completed.
/// </summary>
[EnumMember(Value = "Completed")]
    Completed,
/// <summary>
/// Used when an item is no longer in the work queue.
/// </summary>
[EnumMember(Value = "OnHold")]
    OnHold,
/// <summary>
/// Used when an item is no longer necessary but work may have been done.
/// </summary>
[EnumMember(Value = "Unneeded")]
    Unneeded,
/// <summary>
/// Used when the status cannot be determined.
/// </summary>
[EnumMember(Value = "Unknown")]
    Unknown
}

The attribute, JsonConverter, can be placed on different code items depending on your need. The following is the attribute scope:

  [AttributeUsage(AttributeTargets.Class | AttributeTargets.Enum | AttributeTargets.Field | AttributeTargets.Interface | AttributeTargets.Parameter | AttributeTargets.Property | AttributeTargets.Struct, AllowMultiple = false)]

I hope this helps someone else in the future.



DR Dhivya Rajendran Syncfusion Team July 3, 2018 11:13 AM UTC

Hi Jim, 
Thanks for your update. 
We are happy to hear that your problem has been resolved. 

Please get back to us if you need further assistance from us. 
Regards,
R.Dhivya 



JI JimN July 3, 2018 11:47 AM UTC

After giving the solution a thorough test, I discovered that using the JsonConverter will not work on the first item in an enum, UNLESS you set the value to start at something other than the default, which is zero (0). So, set the first item in the enum = 1, as in the following:

/// <summary>
/// Can be handled at anytime. Minimal impact to business.
/// </summary>
Low = 1,

Also, it does not seem like the [EnumMember...] attribute is even needed on the enum values.


DR Dhivya Rajendran Syncfusion Team July 5, 2018 10:56 AM UTC

Hi Jim, 
Thanks for your update. 

We have validated your query, By default enum values starts from 0 but JsonConverter ignore the value 0 so that the first item is not displayed in Grid. To resolve the reported problem you can set the DefaultValue as null for enum property [weekdays].  

Kindly refer to the below code example for more information. 

[OrderController] 
public class OrdersDetails 
{ 
    public static List<OrdersDetails> order = new List<OrdersDetails>(); 
    public OrdersDetails() 
    { 
 
    } 
    public OrdersDetails(int OrderID, string CustomerId, weekdays val) 
    { 
        this.OrderID = OrderID; 
        this.CustomerID = CustomerId; 
        this.val = val; 
    } 
    public static List<OrdersDetails> GetAllRecords() 
    { 
         order.Add(new OrdersDetails(code + 1, "ALFKI, weekdays.Monday)); 
         order.Add(new OrdersDetails(code + 2, "ANATR”, weekdays.Sunday)); 
         return order; 
    } 
 
    public int? OrderID { get; set; } 
    public string CustomerID { get; set; } 
    // convert int value(0) into enum values (Monday) 
    [Newtonsoft.Json.JsonConverter(typeof(Newtonsoft.Json.Converters.StringEnumConverter))] 
    [DefaultValue(null)]  // accept the enum value as 0 too 
 
    public weekdays val { get; set; } 
} 
public enum weekdays 
{ 
    [EnumMember(Value = "Monday")] 
    Monday, 
    [EnumMember(Value = "Tuesday")] 
    Tuesday, 
} 


Regards,
R.Dhivya 



HI Hiral December 11, 2019 08:40 PM UTC

Hi I also found a great code examples which may help others in the future:

int i = (int)Color.Blue;

//Result: i = 3


Snippet taken from: c# convert int to enum


TS Thavasianand Sankaranarayanan Syncfusion Team December 12, 2019 08:56 AM UTC

Hi Hiral, 

Thanks for your suggestion and valuable feedback. 

Regards, 
Thavasianand S. 


Loader.
Up arrow icon