[英]How to display different name for properties in enum class in @Html.DropDownListFor
Lets say I have the model:假设我有 model:
using System.ComponentModel.DataAnnotations;
namespace MyProject.Models
{
public class InputParametersModel
{
public Variable Xvariable { get; set; }
public Variable Yvariable { get; set; }
}
public enum Variable
{
[Display(Name = "A A")]
aa,
[Display(Name = "B B")]
bb,
[Display(Name = "C C")]
cc,
}
}
and my view has:我的观点是:
@model MyProject.Models.InputParametersModel
@using MyProject.Models
<div>
@using (Html.BeginForm())
{
@Html.DropDownListFor(m => m.Xvariable, new SelectList(Enum.GetValues(typeof(Variable))), "Select", new { @class = "form-control"})
@Html.DropDownListFor(m => m.Yvariable, new SelectList(Enum.GetValues(typeof(Variable))), "Select", new { @class = "form-control"})
}
</div>
How do I get the dropdowns to display "AA", "BB" and "CC"?如何让下拉菜单显示“AA”、“BB”和“CC”? I thought adding
[Display(Name = "AA")]
would solve my problem but it's still displaying 'aa' etc.我认为添加
[Display(Name = "AA")]
可以解决我的问题,但它仍然显示 'aa' 等。
(this just a snippet of the code but hopefully I accounted for any syntax errors) (这只是代码的一个片段,但希望我解释了任何语法错误)
Edit: forgot to copy @using (Html.BeginForm())
编辑:忘记复制
@using (Html.BeginForm())
You can show display names using Html.GetEnumSelectList
in.Net Core您可以使用
Html.GetEnumSelectList
in.Net Core 显示显示名称
@Html.DropDownListFor(m => m.Xvariable,Html.GetEnumSelectList<Variable>(),
new { @class = "form-control" })
//Or
<select asp-for="Xvariable" asp-items="Html.GetEnumSelectList<Variable>()">
<option selected="selected" value="">Please select</option>
</select>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.