我正在尝试使用 Razor 视图中的标签助手在 ASP.NET MVC Core 中创建一个带有枚举属性的下拉列表:
I'm trying to create a dropdown list with an enum property in ASP.NET MVC Core using the tag helper in a Razor view:
这是模型:
public class PersonalMember : Member
{
[Required, Display(Name = "First Name")]
public string FirstName { get; set; }
[Required, Display(Name = "Last Name")]
public string LastName { get; set; }
[EnumDataType(typeof(Gender))]
public Gender GenderType { get; set; }
}
public enum Gender
{
Male = 1,
Female = 2
}
这是视图中的一个表单的一部分:
Here is part of a form in the view:
<div class="form-group">
<label asp-for="GenderType" class="col-md-2 control-label"></label>
<div class="col-md-10">
<select asp-for="GenderType" asp-items="Html.GetEnumSelectList<GenderType>()">
<option selected="selected" value="">Please select</option>
</select>
<span asp-validation-for="GenderType" class="text-danger" />
</div>
</div>
我遇到的问题是在 Html.GetEnumSelectList
之后,GenderType
无法识别并显示为错误.
The problem I'm having is that after Html.GetEnumSelectList
, GenderType
is not recognized and shows up as an error.
有人知道怎么解决吗?
我想你不小心使用了 GenderType
而不是 Gender
.正确的语法是
I think you accidentally used GenderType
instead of Gender
. The correct syntax is
<select asp-for="GenderType" asp-items="Html.GetEnumSelectList<Gender>()">
<option selected="selected" value="">Please select</option>
</select>
这篇关于在 ASP.NET MVC Core 中使用枚举作为下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!