在 Razor 页面(在 ASP.NET Core 中)上迭代枚举值时,如何显示枚举的显示名称?
剃刀页面:
<label asp-for="Survey.ToldNotToTakeHormones"></label><br />
@foreach (var answer in Enum.GetValues(typeof(AnswerYND)))
{
<div class="custom-control custom-radio custom-control-inline ">
<label><input type="radio" asp-for="Survey.ToldNotToTakeHormones" value="@answer" />@answer</label>
</div>
}
剃须刀页面背后的代码:
public class EditModel : PageModel
{
[BindProperty]
public Survey Survey { get; set; }
调查类:
public class Survey
{
[Display(Name = "Have you ever been told by a medical professional not to take hormones?")]
public AnswerYND? ToldNotToTakeHormones { get; set; }
答案YND:
public enum AnswerYND
{
Yes,
No,
[Display(Name="Don't Know")]
DontKnow
}