I have an ENUM like this:
public enum DeliveryMailOptions
{
Regular,
[Display(Name ="FedEx Priority")]
FedExPriority,
[Display(Name ="FedEx Two-Day")]
FedExTwoDay
}
It is used in a data model like so:
[Display(Name = "Mail Option")]
public DeliveryMailOptions MailOption { get; set; }
When rendered, it ignores the Display attribute and only shows the enum text.
Hi Jason,
Indeed the display attributes are ignored for Enums. Thank you for noticing and reporting!
I have added a vote on your behalf to increase the bug report priority. As a small thank you gesture I've also added some Telerik points to your account.
In the meantime, a possible workaround could be to use FormItem Template to set custom editors.
I've created the example below to demonstrate how to use some custom editors including a DropDownList in FormItem Template. You can populate it with Enum data and display the Name properties of the Display attributes. For that purpose, I have used a custom method to check whether Display attribute is defined and get and display its Name property, otherwise display the Enum's member name (for example the first mail option - Regular).
Note: An important note in terms of validation is that when using the template, the built-in validation messages from the Form will not be rendered and you can use the Telerik Validation tools to provide validation messages.
@using System.ComponentModel.DataAnnotations
@using System.Reflection
<TelerikForm Model="@data" Width="300px">
<FormValidation>
<DataAnnotationsValidator />
</FormValidation>
<FormItems>
<FormItem>
<Template>
<label for="address">Address : @data.Address</label>
<TelerikTextBox Id="address" @bind-Value ="@data.Address"></TelerikTextBox>
<TelerikValidationMessage For="@(() => data.Address)"></TelerikValidationMessage>
</Template>
</FormItem>
<FormItem>
<Template>
<label for="delivery">Delivery: @data.MailOption</label>
<TelerikDropDownList Data="@myDdlData"
TextField="MyTextField"
ValueField="MyValueField" @bind-Value="@data.MailOption"
Id="delivery">
</TelerikDropDownList>
<TelerikValidationMessage For="@(() => data.MailOption)"></TelerikValidationMessage>
</Template>
</FormItem>
</FormItems>
</TelerikForm>
@code {
//form model
public MyModel data = new MyModel();
public class MyModel
{
[Required(ErrorMessage = "Address must be provided.")]
public string Address { get; set; }
[Required(ErrorMessage = "Select delivery option.")]
public DeliveryMailOptions MailOption { get; set; }
}
//dropdownlist model
public class EnumDdlModel
{
public DeliveryMailOptions MyValueField { get; set; }
public string MyTextField { get; set; }
}
List<EnumDdlModel> myDdlData { get; set; } = new List<EnumDdlModel>();
protected override void OnInitialized()
{
//prepare instances of the model from the list of enum values and a desired string representation for the user
foreach (DeliveryMailOptions item in Enum.GetValues(typeof(DeliveryMailOptions)))
{
myDdlData.Add(new EnumDdlModel {MyTextField = GetDisplayName(item), MyValueField = item });
}
base.OnInitialized();
}
public enum DeliveryMailOptions
{
Regular,
[Display(Name = "FedEx Priority")]
FedExPriority,
[Display(Name = "FedEx Two-Day")]
FedExTwoDay
}
//custom method to check if a Display Attribute exists and return its Name property,
//otherwise fallback to the enum’s member name instead.
public string GetDisplayName(Enum val)
{
return val.GetType()
.GetMember(val.ToString())
.FirstOrDefault()
?.GetCustomAttribute<DisplayAttribute>(false)
?.Name
?? val.ToString();
}
}
Regards,
Nadezhda Tacheva
Progress Telerik
Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.