【发布时间】:2017-02-24 10:54:17
【问题描述】:
我在我的 Razor 视图中使用带有选择标记的 asp-items="@Html.GetEnumSelectList(typeof(Salary))",以根据 enum Salary 填充列表值。
但是,我的枚举包含一些我想在其中包含空格的项目。例如。其中一项是PaidMonthly,但是当我使用Html.GetEnumSelectList 显示它时,我希望它显示为"Paid Monthly"(其中有一个空格)
I tried using the Description attribute over each member in the enum, however when the select box renders it uses the raw value only.
谁能帮我解决这个问题?
(我的代码示例)-> 使用 ASP.NET Core 1.0
剃刀视图:
<select asp-for="PersonSalary" asp-items="@Html.GetEnumSelectList(typeof(Enums.Salary))">
</select>
枚举工资:
public enum Salary
{
[Description("Paid Monthly")]
PaidMonthly = 1,
PaidYearly = 2
}
【问题讨论】:
-
显示你尝试过的代码
-
试试
[Display(Name = "Paid Monthly")] -
其实我也试过了。虽然不起作用:\
-
试试斯蒂芬所说的然后使用
@Html.EnumDropDownListFor(model => model.Salary),假设你的列表是一个下拉列表... -
嗨,Jay,不幸的是,我似乎无法在我的 razor 视图中找到“Html.EnumDropDownListFor”的定义。 PS:我使用的是 ASP.NET Core 1.0
标签: c# html razor enums asp.net-core-mvc