【问题标题】:Issues in enum values in MVC4MVC4 中的枚举值问题
【发布时间】:2014-01-28 06:59:40
【问题描述】:

大家好,我正在尝试使用枚举值创建一个下拉列表。我的 html 页面中有一个选择字段,例如,

   <select name="Mem_BloodGr" >
   <option value="A+">A+</option><option value="A-">A-</option>
   <option value="B+">B+</option><option value="B-">B-</option>
   <option value="O+">O+</option><option value="O-">O-</option>
   <option value="AB+">AB+</option><option value="AB-">AB-</option>
   </select>

这在我网页的许多地方重复出现。所以我尝试使用枚举值生成下拉列表

namespace .....Models
{
   public class MemberData
   {
    public int Id { get; set; }
    public string Mem_NA { get; set; }
    ........
    public BloodGroup Mem_BloodGr { get; set; }
   }

  public enum BloodGroup
   {
    A+,                //// **error shows here like, "} expected"**
    A-,
    B-,
    B+

   }
 }

但添加枚举值时出现错误。任何人都可以帮助我。这是在 MVC4 中创建这种类型下拉列表或任何其他简单方法的正确方法吗???

【问题讨论】:

  • 枚举值中不能有 + 和 -。改用 APlus 之类的。
  • 我认为this answer 对你有用。
  • @HåkanFahlstedt:或者我也不能使用“空格”。对.....但是我想要这样的输出..这是在MVC4中创建重复使用下拉列表的任何替代方法

标签: c# asp.net-mvc asp.net-mvc-4 enums


【解决方案1】:

这是我的枚举下拉方法:

制作我们自己的属性:

public class EnumDescription : Attribute
{
    public string Text { get; private set; }

    public EnumDescription(string text)
    {
        this.Text = text;
    }
}

创建一个辅助类:

public static class SelectListExt
{
    public static SelectList ToSelectList(Type enumType)
    {
        return ToSelectList(enumType, String.Empty);
    }

    public static SelectList ToSelectList(Type enumType, string selectedItem)
    {
        List<SelectListItem> items = new List<SelectListItem>();

        foreach (var item in Enum.GetValues(enumType))
        {
            FieldInfo fi = enumType.GetField(item.ToString());
            var attribute = fi.GetCustomAttributes(typeof(EnumDescription), true).FirstOrDefault();
            var title = attribute == null ? item.ToString() : ((EnumDescription)attribute).Text;

            // uncomment to skip enums without attributes
            //if (attribute == null)
            //    continue;

            var listItem = new SelectListItem
            {
                Value = ((int)item).ToString(),
                Text = title,
                Selected = selectedItem == ((int)item).ToString()
            };
            items.Add(listItem);
        }

        return new SelectList(items, "Value", "Text", selectedItem);
    }
}

枚举本身:

public enum DaylightSavingTime
{
    [EnumDescription("Detect automatically")]
    Auto = 0,
    [EnumDescription("DST always on")]
    AlwaysOn = 1,
    [EnumDescription("DST always off")]
    AlwaysOff = 2
}

你可以把你想要的一切都放在 EnumDescription 中

用法:

@Html.DropDownList(
    "dst",
    SelectListExt.ToSelectList(typeof(DaylightSavingTime)),
    new {
        id = "dst",
        data_bind = "value: Dst"
    })

注意:data_bind 被转换为“data-bind”。有用的是你使用 Knockout.js

您可以省略最新的新 {} 块

【讨论】:

    【解决方案2】:

    枚举值中不能有 + 和 -,只能是字母和数字。改用 APlus 之类的。您可以使用枚举作为下拉列表的来源,但我会推荐一个具有名称的对象列表。这样,您可以更好地控制显示的内容,并且可以在名称中使用任何字符。使用类似这样的东西来填充下拉列表:

    public class BloodGroup
    {
      public string Name {get; set; }
      // other properties
    }
    

    然后您可以创建一个可以在视图中重复使用的 BloodGroup 对象列表:

    var BloodGroupList = new List<BloodGroup> { new BloodGroup { Name = "A+"}, new BloodGroup { Name = "A-"}, ... };
    

    在你看来:

    @Html.DropDownList("BloodGroups", BloodGroupList.Select(b => new SelectListItem { Name = b.Name, Value = b.Name }))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-10
      • 2023-04-04
      • 1970-01-01
      相关资源
      最近更新 更多