Using enum for dropdown list in ASP.NET MVC Core(在 ASP.NET MVC Core 中使用枚举作为下拉列表)
问题描述
我正在尝试使用 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 中使用枚举作为下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 ASP.NET MVC Core 中使用枚举作为下拉列表


- CanBeNull和ReSharper-将其用于异步任务? 2022-01-01
- Windows 喜欢在 LINUX 中使用 MONO 进行服务开发? 2022-01-01
- Azure Active Directory 与 MVC,客户端和资源标识同一 2022-01-01
- 是否可以在 .Net 3.5 中进行通用控件? 2022-01-01
- 带问号的 nvarchar 列结果 2022-01-01
- 在 C# 中异步处理项目队列 2022-01-01
- 在 LINQ to SQL 中使用 contains() 2022-01-01
- C# 通过连接字符串检索正确的 DbConnection 对象 2022-01-01
- 使用 rss + c# 2022-01-01
- 为什么 C# 中的堆栈大小正好是 1 MB? 2022-01-01