c# - Html.GetEnumSelectList - 获取带空格的枚举值

标签 c# html razor enums asp.net-core-mvc

我在我的 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/

相关文章:

javascript - Angular Material : show menu icon when sidenav collapses

c# - 使用对象初始值设定项创建实例的表达式

c# - 松散组合比较

html - 从语义上讲,我可以将 <hr> 放在 HTML 中的 <nav> 元素中吗?

asp.net-mvc - 如何在分组列表中发布已编辑项目的模型?

asp.net-mvc-3 - MVC Razor-创建/编辑 View 最佳实践

c# - 如何从 MVC4 中的 View 编辑父项的属性?

c# - 通过环回在 C# 中发送 UDP 数据包。为什么我无法追踪他们?

c# - 尝试将变量从 C# 插入 MySQL 时出错

基于 css 的页面中的 Javascript 随机图像生成器