dalia
dalia

Reputation: 1

How to obtain the Name parameter in tables with Enum, used ej2.syncfusion and asp.net core?

How to show the Name parameter in tables with ej2.syncfusion and asp.net core?

My model is:

public class MyEntity
    {

        public int ID;
        
        public enum StateEnum
        {
            [EnumMember(Value = "State A")]
            [Display(Name = "State A")]
            A = 0,
            [EnumMember(Value = "State B")]
            [Display(Name = "State B")]
            B= 1
        }

   public StateEnum StateEnum{ get; set; }

}

I get the data in the controller to feed the table:

 public IActionResult Index()
        {
            var myEntities= _context.MyEntities.ToList();
            return View(myEntities);
        }

In View I use a GRID with ej2.syncfusion:

<ejs-grid id="Grid" dataSource="@Model" allowPaging="true" allowFiltering="true" allowSorting="true">
    <e-grid-filterSettings type="Menu"></e-grid-filterSettings>
    <e-grid-pagesettings pageSize="4"></e-grid-pagesettings>
    <e-grid-columns>
        <e-grid-column headerText="ID" field="ID" textAlign="Right" width="120" isPrimaryKey="true"></e-grid-column>
        <e-grid-column headerText="StateEnum" field="StateEnum" textAlign="Right" width="120" ></e-grid-column>
       
    </e-grid-columns>

</ejs-grid>

I get a table as a result:

ID | StateEnum
----------------
1  | 0
2  | 1

But I wanted to get the display names from the enumeration:

ID | StateEnum
----------------
1  | State A
2  | State B

Someone can help me?

Upvotes: 0

Views: 297

Answers (2)

thiyagu subramaniyam
thiyagu subramaniyam

Reputation: 11

Based on your query we understand that you want to display the names (State A and State D instead of A and B) from the enumeration. To achieve this requirement we suggest converting Enum to string value using Newtonsoft decorator (StringEnumConverter) like below. Refer to the below code example.

public long ID;     
[Newtonsoft.Json.JsonConverter(typeof(Newtonsoft.Json.Converters.StringEnumConverter))]
            
public StateEnum StateEnum { get; set; }

Upvotes: 0

bugrakosen
bugrakosen

Reputation: 603

You can find the answer on this question. Or you can use static class instead of enum.

public static class StateEnum
{
    public const int A = 0;
    public const int B = 1;
}

Upvotes: 0

Related Questions