c# - 如果 Enum.GetValues(typeof()) 的枚举值在 Model 中声明,为什么无法识别它们?
问题描述
我想在 MVC 中使用枚举使用性别值填充下拉列表,但 Enum.GetValues(typeof(...) 没有返回值。这是 .cshtml 部分:
<div class="form-group">
@Html.LabelFor(m => m.parGender, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.DropDownListFor(m => m.parGender, new SelectList(Enum.GetValues(typeof(Gender))), new { @class = "form-control" })
</div>
</div>
这是这个模型:
[Required(ErrorMessage = "Select your gender!")]
[Display(Name = "Gender:")]
public Gender parGender { get; set; }
public enum Gender
{
Male,
Female
}
我错过了什么?
解决方案
您需要将结果转换为您想要的实际数组类型
(Gender[])Enum.GetValues(typeof(Gender))
推荐阅读
- c++ - 涉及默认复制构造函数的代码应该有段错误,但工作得很好
- python - 从字典中获取 key_tags?
- java - 在 mytheme 上编译 sass 时出错
- sql - sql获取多列的总价值
- javascript - 将 Blogger 帖子正文分成两半
- xml - 使用python 3在xml中的子标签开头添加属性
- c# - IdentityServer 使用外部提供者登录不适用于 long login_hint 或 acr_values
- javascript - 使用脚本标签延迟加载 HTML 小部件
- java - Java DbUnit 信息架构 MySQL
- java - 使用 Mockito 的最佳实践