• <i id='K9jIL'><tr id='K9jIL'><dt id='K9jIL'><q id='K9jIL'><span id='K9jIL'><b id='K9jIL'><form id='K9jIL'><ins id='K9jIL'></ins><ul id='K9jIL'></ul><sub id='K9jIL'></sub></form><legend id='K9jIL'></legend><bdo id='K9jIL'><pre id='K9jIL'><center id='K9jIL'></center></pre></bdo></b><th id='K9jIL'></th></span></q></dt></tr></i><div id='K9jIL'><tfoot id='K9jIL'></tfoot><dl id='K9jIL'><fieldset id='K9jIL'></fieldset></dl></div>
  • <tfoot id='K9jIL'></tfoot>
      <bdo id='K9jIL'></bdo><ul id='K9jIL'></ul>

      <legend id='K9jIL'><style id='K9jIL'><dir id='K9jIL'><q id='K9jIL'></q></dir></style></legend>

        <small id='K9jIL'></small><noframes id='K9jIL'>

      1. 在 ASP.NET MVC Core 中使用枚举作为下拉列表

        Using enum for dropdown list in ASP.NET MVC Core(在 ASP.NET MVC Core 中使用枚举作为下拉列表)
          <bdo id='F4oVx'></bdo><ul id='F4oVx'></ul>
        • <tfoot id='F4oVx'></tfoot>

            <tbody id='F4oVx'></tbody>
        • <small id='F4oVx'></small><noframes id='F4oVx'>

          <i id='F4oVx'><tr id='F4oVx'><dt id='F4oVx'><q id='F4oVx'><span id='F4oVx'><b id='F4oVx'><form id='F4oVx'><ins id='F4oVx'></ins><ul id='F4oVx'></ul><sub id='F4oVx'></sub></form><legend id='F4oVx'></legend><bdo id='F4oVx'><pre id='F4oVx'><center id='F4oVx'></center></pre></bdo></b><th id='F4oVx'></th></span></q></dt></tr></i><div id='F4oVx'><tfoot id='F4oVx'></tfoot><dl id='F4oVx'><fieldset id='F4oVx'></fieldset></dl></div>
          1. <legend id='F4oVx'><style id='F4oVx'><dir id='F4oVx'><q id='F4oVx'></q></dir></style></legend>

                1. 本文介绍了在 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 中使用枚举作为下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Performance overhead of using attributes in .NET(在 .NET 中使用属性的性能开销)
                  Accessing attribute info from DTE(从 DTE 访问属性信息)
                  c# Hide a property in datagridview with datasource(c#使用数据源隐藏datagridview中的属性)
                  Extract Display name and description Attribute from within a HTML helper(从 HTML 帮助器中提取显示名称和描述属性)
                  C# Attributes and their uses(C# 属性及其用途)
                  C# - Getting all enums value by attribute(C# - 按属性获取所有枚举值)

                  1. <i id='aQ6n9'><tr id='aQ6n9'><dt id='aQ6n9'><q id='aQ6n9'><span id='aQ6n9'><b id='aQ6n9'><form id='aQ6n9'><ins id='aQ6n9'></ins><ul id='aQ6n9'></ul><sub id='aQ6n9'></sub></form><legend id='aQ6n9'></legend><bdo id='aQ6n9'><pre id='aQ6n9'><center id='aQ6n9'></center></pre></bdo></b><th id='aQ6n9'></th></span></q></dt></tr></i><div id='aQ6n9'><tfoot id='aQ6n9'></tfoot><dl id='aQ6n9'><fieldset id='aQ6n9'></fieldset></dl></div>
                  2. <tfoot id='aQ6n9'></tfoot>
                    <legend id='aQ6n9'><style id='aQ6n9'><dir id='aQ6n9'><q id='aQ6n9'></q></dir></style></legend>
                    • <bdo id='aQ6n9'></bdo><ul id='aQ6n9'></ul>

                          <small id='aQ6n9'></small><noframes id='aQ6n9'>

                              <tbody id='aQ6n9'></tbody>