I think this was a pretty elegant solution. I will use one of my own examples to demonstrate. I have a Model where one of the fields needs to be selected from an enumeration. The Model is called DonationMaterials and the property is Availability
public string? Availability { get; set; } = String.Empty;
This property can have 3 values; Available, Not Available, or Unknown. I created a public static method under the model called AvailabilityOptions
public static IEnumerable<SelectListItem>? AvailabilityOptions()
{
return new[]
{
new SelectListItem { Text = "Available", Value = "Available"},
new SelectListItem { Text = "Not Available", Value = "Not Available"},
new SelectListItem { Text = "Unknown", Value = "Unknown"}
};
}
Once I created the static method, anytime I need to fill the SelectList I simply use the code in the cshtml for the page. You will need to include the Namespace of the model to have accessibility to the static method. This is clean. You could also answer the enumeration from the static method and use code to convert the enumeration into a collection of SelectListItem's
<select asp-for="DonationMaterials.Availability" asp-items="DonationMaterials.AvailabilityOptions()" class="form-control"></select>
The beauty of this approach is that the enumeration is really a Domain (mode) specific data and it keeps it where it belongs (in the model). The use of the model in the interface is quite simple.