We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

How to display enum display names in grid control view

Hi there,I'd like to know how to display enum display names instead of their int values in grid control.When creating a simple table in ASP.net core, we simply pass the model like so: @item.DayOfWeek and it will display their textual values "monday" to "sunday", which are defined in the ViewModel, not 0 to 6...I'd like to know how to achieve the same goal using the syncfusion asp.net core grid control.Thanks in advance! (And btw, how can we format text, it messes it up every time I save :(

12 Replies

VA Venkatesh Ayothi Raman Syncfusion Team March 31, 2017 11:57 AM UTC

Hi Philippe, 
Thanks for contacting Syncfusion support. 
Query #1:” Display enum text values not integer values” 
We have achieved your requirement using Newtonsoft serializer. Because, JavascriptSerializer serializes the enum to numeric values only. To serialize the enum values to their string representation using Newtonsoft.json and we have achieved your requirement “Serializes the enum to their string representation in syuncfusion Grid” using custom serialize in datamanager. Please refer to the following code example, 
Code example
@grid 
 
<ej-grid id="FlatGrid" allow-paging="true" > 
        . . . 
     
    <e-columns> 
              . . . 
        <e-column field="val" header-text="val" width=@("80px")></e-column> 
    </e-columns> 
</ej-grid> 
 
@server side 
   public IActionResult Index() 
        { 
            DataManagerConverter.Serializer = new DMSerial(); 
              . . . 
            return View(); 
        } 
 
public class DMSerial : IDataSourceSerializer 
        { 
            public string Serialize(object obj) 
            { 
                var str = Newtonsoft.Json.JsonConvert.SerializeObject(obj); 
                return str; 
            } 
        } 
 
@enum declaration 
        public enum weekdays { 
            [EnumMember(Value="Monday")] 
            Monday, 
            [EnumMember(Value = "Tuesday")] 
            Tuesday, 
            [EnumMember(Value = "Wednesday")] 
            Wednesday, 
            [EnumMember(Value = "Thursday")] 
            Thursday, 
            [EnumMember(Value = "Friday")] 
            Friday, 
            [EnumMember(Value = "Saturday")] 
            Saturday, 
            [EnumMember(Value = "Sunday")] 
            Sunday, 
        } 
@List 
public void BindDataSource() 
        { 
            int code = 10000; 
            for (int i = 1; i < 10; i++) 
            { 
                order.Add(new Orders(code + 1, "ALFKI", i + 0, 2.3 * i, new DateTime(1991, 05, 15), "Berlin", weekdays.Monday)); 
                order.Add(new Orders(code + 2, "ANATR", i + 2, 3.3 * i, new DateTime(1990, 04, 04), "Madrid",weekdays.Thursday)); 
                order.Add(new Orders(code + 3, "ANTON", i + 1, 4.3 * i, new DateTime(1957, 11, 30), "Cholchester",weekdays.Tuesday)); 
                order.Add(new Orders(code + 4, "BLONP", i + 3, 5.3 * i, new DateTime(1930, 10, 22), "Marseille",weekdays.Friday)); 
                order.Add(new Orders(code + 5, "BOLID", i + 4, 6.3 * i, new DateTime(1953, 02, 18), "Tsawassen",weekdays.Sunday)); 
                code += 5; 
            } 
        } 

We have also prepared a sample for your convenience which can be download from following link, 
Query #2: how can we format text, it messes it up every time I save 
We have unclear about your requirement could you please share more details about your requirement? 
Regards, 
Venkatesh Ayothiraman. 



PM Philippe-Antoine Major March 31, 2017 05:20 PM UTC

First of all thanks for the answer, I'll test it later today.

As for my second query, I was just wondering why the text I posted above did not respect linebreaks etc.... It's not  a real "query", you don't have to answer it.


PM Philippe-Antoine Major March 31, 2017 06:06 PM UTC

Do we have to absolutely use the datamanager or can we simply use the datasource attribute of the grid tag helper and send the required data from ef core/repository directly?


AS Alan Sangeeth S Syncfusion Team April 3, 2017 08:54 AM UTC

Hi Philippe, 
 
You can simply use the datasource attribute of grid taghelper to bind data to grid control. We have modified the sample that can be  
downloadable from following link. 
 
 
Regards,
Alan Sangeeth S
 



PM Philippe-Antoine Major April 3, 2017 03:44 PM UTC

Ok nice! Thanks for the help. It works really well!

I'm wondering though if there could be a more optimal approach to it.

Would you know if it would be possible to use a "custom" stringEnumConverter to be able to reuse the already present [Display(Name="<name>")] data annotations in my models instead of having to duplicate the same thing twice (once for the json converter and once for the default asp.net core view rendering which uses the Display annotation and not the EnumMember).

Thanks again for the help and hope you have an idea!




AS Alan Sangeeth S Syncfusion Team April 4, 2017 10:01 AM UTC

Hi Philippe, 
 
Thanks for the update. 
 
In previous updates, we have provided sample with DataAnnoation as EnumMember for properties of enum. You can also provide it as Display Annotation.  

public enum weekdays { 
            [Display(Name ="Monday")] 
            Monday, 
            [Display(Name = "Tuesday")] 
            Tuesday, 
 
} 

We have modified the sample that can be downloadable from below link. 

Regards, 
Alan Sangeeth S 



PM Philippe-Antoine Major April 4, 2017 12:24 PM UTC

Hi Alan, thanks for the reply.

Sadly, it seems like the provided solution doesn't work as expected.

To confirm this, please change one of the enumeration Display data annotations to something else than the default value descriptor. For example, change [Display(Name = "Monday")] to [Display(Name = "This is Monday")] and start the application.

In my environment at least, it still displays "Monday" only.... Is that what you also get?

And if so, that means the provided solution doesn't work, would you have another idea?

On my side, I made a "work-around" but I don't know if it's the optimal approach and I'd like to get  your advice on it as I imagine there could be a better way of doing this.I created a custom StringEnumConverter and use this in place of the default one. See the provided file.

What do you think about it, is there something wrong with what I did when trying your solution? Or is it also not working on your side as expected when the display attribute is not the same as the enum descriptor?

Also just to note, the problem I was talking about in my first question is still appearing whenever we edit a post reply, it messes up the text formatting.


Attachment: OverridenStringEnumConverter_ddbb5b39.7z


VA Venkatesh Ayothi Raman Syncfusion Team April 5, 2017 11:21 AM UTC

Hi Philippe, 
Thanks for the update. 
We suggest you to use DataAnnoation as EnumMember for properties of enum value. In this, we  can easily get the enum values without any serializing/deserializing the value. Please refer to the following code example, 
Code example
public enum weekdays { 
            [EnumMember(Value = "This is Monday")] 
            Monday, 
            [EnumMember(Value = "This is Tuesday")] 
            Tuesday, 
            [EnumMember(Value = "This is Wednesday")] 
            Wednesday, 
            [EnumMember(Value = "This is Thursday")] 
            Thursday, 
            [EnumMember(Value = "This is Friday")] 
            Friday, 
            [EnumMember(Value = "This is Saturday")] 
            Saturday, 
            [EnumMember(Value = "This is Sunday")] 
            Sunday, 
        } 
Output
 
If we use data annotation as Display for enum values, then we need to write the function for get the enum values like your workaround. Please refer to the online forum for get the Display values, 

Regards, 
Venkatesh Ayothiraman. 



PM Philippe-Antoine Major April 5, 2017 04:36 PM UTC

Ok, thanks for the help! And for those that still want to use Display(Name="...") to avoid duplicate code, they can review the file I uploaded above.


VA Venkatesh Ayothi Raman Syncfusion Team April 6, 2017 12:40 PM UTC

Hi Philippe, 
Thanks for the update. 
There is no JavaScript serializer in ASP.NET core app. So, if we use DataAnnoation as DisplayName for properties of enum then we need to override the stringEnumConverter like your workaround. 
In previous post, we have been told to use custom serialize for serialize the enum values to string representation using Newtonsoft.json. But there is no need to use custom serialize for enum values to string representation. Becuase ASP.Net core app has built-in support for Newtonsoft serializer.   
Regards, 
Venkatesh Ayothiraman. 



PM Philippe-Antoine Major April 7, 2017 12:36 PM UTC

Effectively, it is not mandatory to overwrite the default implementation but if you need the enum somewhere else and use something like @Html.GetEnumSelectList, then it uses display attribute if I'm not mistaken so that means I would need to write both Display(Name="x") and [EnumMember(Value="x"), which is duplicate code in a sense to me. :)



FS Farveen Sulthana Thameeztheen Basha Syncfusion Team April 13, 2017 12:59 PM UTC

Hi Philippe, 

While you are using @Html.GetEnumSelectList, Display DataAnnoation has been explicitly passed and it gets works when it used in @Html.GetEnumSelectList. When we use normal textbox or other attributes, Display DataAnnoation won’t get work. Likewise in Grid, Display DataAnnoation doesn’t get support. To overcome this problem, we suggested you to use DataAnnoation as EnumMember in our previous update. When you use DataAnnoation as EnumMember we  can easily get the enum values without any serializing/deserializing the value.  Please refer to the sample using EnumMember in previous update. 
 
Please get back to us if you need any further assistance. 

Regards, 

Farveen sulthana T 


Loader.
Live Chat Icon For mobile
Up arrow icon