Property Enum
When you need to define a property that can have a limited number of values, you can add an Enum property using the method shown below. For instance, in this example, we demonstrate how to configure an enum property.
The TrainingStatus property is defined using AddEnumProperty<TrainingStatus>and renders as a dropdown (<select>) in the user interface, allowing users to select from a predefined set of values.
This method works with standard C# enums and supports all general configuration methods available for properties rendered as dropdowns. In addition to the common configuration options, it has one special behavior if the enum members are decorated with the [Display] attribute (like in below example).:
publicenumTrainingStatus
{
[Display(Name="Not Started",Description="Training not started yet")]
Plan,
[Display(Name="In Progress",Description="Training in progress")]
InProgress,
[Display(Name="Completed",Description="Training completed")]
Completed,
}
✅ If the enum members are decorated with the [Display] attribute (from System.ComponentModel.DataAnnotations), the framework will:
- Use the Display(Name) value as the option label in the dropdown.
- Use the Display(Description) value as the tooltip for each dropdown item.
publicpartialclassEmployee:IBkEntity<Employee,ApplicationDbContext>
{
publicvoidBkConfiguration(IBkEntityDesigner<Employee,ApplicationDbContext>designer)
{
// Note: The AddEnumProperty
{
// Sets the default selected value and whether the user can change it. c.DefaultValue(TrainingStatus.Plan,BkCanUserChangeValue.Yes);
// Sets the label text that appears next to the dropdown. c.Label("Training Status");
// Sets a placeholder for the select editor, shown when no value is selected (only useful if enum is nullable). c.PlaceHolder("Select training status");
// Adds a tooltip when hovering over the dropdown editor. c.ToolTip("Select the current training status");
// Applies a custom CSS class to the dropdown editor. c.CssClass("training-status-select");
// Hides the editor from the form by default. c.HideEditor();
// Hides the column for this property in the grid view. c.HideGridViewColumn();
// Sets the tab order for the editor in the form. c.TabIndex(5);
// Adds a custom validation rule based on the enum value.// For example: Completed can only be selected if progress is above 80%. c.ValidationRuleValue(value=>value!=TrainingStatus.Completed,"Training cannot be marked as completed directly","ValidateTrainingStatus");
// Adds a validation rule using the full model. c.ValidationRuleModel(model=>model.TrainingStatus!=TrainingStatus.Completed||model.Progress>=80,"Training can only be completed if progress is at least 80%","ValidateStatusAgainstProgress");
// Adds a validation rule using the database context and model. c.ValidationRuleUsingDbContext((dbContext,model)=>
{
// For example: prevent multiple 'In Progress' trainings for the same person. return!dbContext.Trainings.Where(x=>x.EmployeeId==model.EmployeeId&&x.Id!=model.Id).Any(x=>x.TrainingStatus==TrainingStatus.InProgress);
},"Another training is already in progress for this employee","ValidateSingleInProgressPerEmployee");
// Applies a built-in validation attribute such as Required. c.ValidationRuleUsingExistingAttribute(newRequiredAttribute());
});
}
}