我在我的 Razor View 中使用带有选择标记的 asp-items="@Html.GetEnumSelectList(typeof(Salary))"
来根据 枚举填充列表值工资
。
但是,我的枚举包含一些我希望在其中有空格的项目。例如。其中一项是 PaidMonthly
,但是当我使用 Html.GetEnumSelectList
显示它时,我希望它显示为 "Paid Monthly"
(其中有一个空格)
我尝试使用 Description
在枚举中的每个成员上属性,但是当Select框呈现时,它仅使用原始值。
谁能帮我解决这个问题?
(我的代码示例)-> 使用 ASP.NET Core 1.0
Razor View :
<select asp-for="PersonSalary" asp-items="@Html.GetEnumSelectList(typeof(Enums.Salary))">
</select>
枚举工资:
public enum Salary
{
[Description("Paid Monthly")]
PaidMonthly = 1,
PaidYearly = 2
}
最佳答案
我设法解决了它。我只需要使用 GetEnumSelectList<>
的另一种方法, 在 Razor View 中我们需要使用 Display 属性。
代码如下:
Razor View :
<select asp-for="PersonSalary" asp-items="Html.GetEnumSelectList<Enums.Salary>()"></select>
枚举工资:
public enum Salary
{
[Display(Name="Paid Monthly")]
PaidMonthly = 1,
PaidYearly = 2
}
关于c# - Html.GetEnumSelectList - 获取带空格的枚举值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40052794/