Html.GetEnumSelectList - Get Enum values โ€‹โ€‹with spaces

I used asp-items="@Html.GetEnumSelectList(typeof(Salary))" in my Razor view with a select tag to populate the list values โ€‹โ€‹based on enum Salary .

However, my listing contains some elements in which I would like to have spaces inside. For instance. one of the elements of PaidMonthly , but when I show it using Html.GetEnumSelectList , I would like it to display as "Paid Monthly" (with a space in it)

I tried to use the Description attribute for each member in the enumeration, but when the select box displays it, it uses only the original value.

Can anyone help me with this issue?

(sample of my code) -> Using ASP.NET Core 1.0

Shaver Type:

 <select asp-for="PersonSalary" asp-items="@Html.GetEnumSelectList(typeof(Enums.Salary))"> </select> 

Salary Enum:

 public enum Salary { [Description("Paid Monthly")] PaidMonthly = 1, PaidYearly = 2 } 
+5
source share
1 answer

I managed to solve it. I just had to use another GetEnumSelectList<> method, and in the Razor view we need to use the Display attribute.

Here is the code:

Shaver Type:

 <select asp-for="PersonSalary" asp-items="Html.GetEnumSelectList<Enums.Salary>()"></select> 

Salary Enum:

 public enum Salary { [Display(Name="Paid Monthly")] PaidMonthly = 1, PaidYearly = 2 } 
+12
source

Source: https://habr.com/ru/post/1258235/


All Articles