Unplanned
Last Updated: 04 Mar 2024 12:39 by Kristjan
MichaƂ
Created on: 31 May 2021 13:00
Category: UI for Blazor
Type: Bug Report
11
Enum DisplayName attributes are ignored in view mode of the Grid

When filtering or editing a Grid with enum data, the Name property of their Display parameter is respected.

However, in the initial view mode of the Grid the Name property is not applied and the enum values are rendered regardless of whether or not their Display parameter has a Name property defined.

 

==========

ADMIN EDIT

==========

In the meantime, a workaround you might try is to create a custom method to check whether Display attribute is defined and get and display its Name property, otherwise display the Enum's member name.

You can then use a Template for the column that uses enum data, cast its context to the model you are using and invoke the method on the field containing the enum. The sample below demonstrates how you can achieve this.

@using System.ComponentModel.DataAnnotations
@using System.Reflection

<TelerikGrid Data=@MyData EditMode="@GridEditMode.Inline" Pageable="true" Height="500px"
             OnUpdate="@UpdateHandler" FilterMode="@GridFilterMode.FilterRow">
    <GridColumns>
        <GridColumn Field=@nameof(SampleData.ID) Editable="false" Title="ID" />
        <GridColumn Field=@nameof(SampleData.Name) Title="Name" />
        <GridColumn Field=@nameof(SampleData.Role) Title="Position">       
            <Template>
                @{
                    var currentEmployee = context as SampleData;
                    var currentRole = GetDisplayName(currentEmployee.Role);                        
                }

                @currentRole
            </Template>
        </GridColumn>
        <GridCommandColumn>
            <GridCommandButton Command="Save" Icon="save" ShowInEdit="true">Update</GridCommandButton>
            <GridCommandButton Command="Edit" Icon="edit">Edit</GridCommandButton>
        </GridCommandColumn>
    </GridColumns>
</TelerikGrid>

@code {
    //custom method to check whether Display attribute is defined and get and display its Name property, otherwise display the Enum's member name
    public string GetDisplayName(Enum val)
    {
        return val.GetType()
                  .GetMember(val.ToString())
                  .FirstOrDefault()
                  ?.GetCustomAttribute<DisplayAttribute>(false)
                  ?.Name
                  ?? val.ToString();
    }


    public void UpdateHandler(GridCommandEventArgs args)
    {
        SampleData item = (SampleData)args.Item;

        //update the view-model
        var index = MyData.FindIndex(i => i.ID == item.ID);
        if (index != -1)
        {
            MyData[index] = item;
        }

        //perform actual data source operations here
    }

    //model and dummy data generation
    public class SampleData
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public Role Role { get; set; }
    }

    public enum Role
    {
        [Display(Name = "Manager")]
        ManagerRole,
        [Display(Name = "Employee")]
        EmployeeRole,
        [Display(Name = "Contractor")]
        ContractorRole
    }

    public List<SampleData> MyData { get; set; }

    protected override void OnInitialized()
    {
        MyData = new List<SampleData>();

        for (int i = 0; i < 50; i++)
        {
            MyData.Add(new SampleData()
            {
                ID = i,
                Name = "name " + i,
                Role = (Role)(i % 3) // just some sample to populate initial values for the enum
            });
        }
    }
}

1 comment
Kristjan
Posted on: 04 Mar 2024 12:39
Would be nice to not have to do the workaround...