【问题标题】:CheckBoxList for Enum types MVC Razor枚举类型 MVC Razor 的 CheckBoxList
【发布时间】:2013-08-06 18:19:32
【问题描述】:

在我的 c#.net MVC 应用程序中,我想显示枚举类型的复选框列表。

我有一个枚举类型

[Flags]
public enum ModeType
{
Undefined = 0,
Read= 1,
Edit= 2
  }

我的模型是

Public TrainingModel
   {
         public int UserID {get;set;}
         public ModeType Type {get;set}
   }

在我看来,我需要两个复选框,一个用于读取,另一个用于编辑 所以我尝试了

    @Html.CheckBoxFor(m => m.Type== ModeType.Read)
@Html.CheckBoxFor(m => m.Type== ModeType.Edit)

但这给了我错误 "模板只能用于字段访问、属性访问、一维数组索引或单参数自定义索引器表达式。"

我通过向我的模型添加另外两个属性来解决这个问题

 Public TrainingModel
   {
         public int UserID {get;set;}
         public ModeType Type {get;set}
         public bool IsRead
         {
           get{Type.HasFlag(ModeType.Read);}
           set{Type |=ModeType.Read;}
         }
         public bool IsEdit
         {
           get{Type.HasFlag(ModeType.Edit);}
           set{Type |=ModeType.Edit;}
         }

   }

然后发表我的看法

@Html.CheckboxFor(m => m.IsRead)
@Html.CheckboxFor(m => m.IsEdit)

我知道我处理它的方式是不正确的,应该有更好的方法来实现这一点。 有人可以给我建议吗。

【问题讨论】:

  • 除了缺少 ViewModel 之外,我没有看到更好的方法。 HTML 本身不会处理标志复选框,它只留下 2 个选项:您所做的(在服务器端处理单独的复选框)和使用管理复选框(未映射)和隐藏输入(映射)的 javascript 添加选定的标志值并设置该输入.在 2 个选项中,您的选项更干净。 javascript 的唯一优势是减少 POST 数据。
  • @Pluc 感谢您的回复。但这是否意味着如果我要向我的 EnumType 添加更多模式,那么我也必须添加属性。
  • 是的。我想您还可以破解一些迭代枚举中的每个选项并生成复选框的东西,然后从表单结果(未绑定到您的模型)中收集响应以将它们相加并将结果保存在您的模型中。它不会那么干净,但它会随着您的枚举更改而扩展。但老实说,如果您计划添加枚举选项,您可能想要选择它们显示的顺序(视图),当您触摸视图时,在视图模型中更改它并不难

标签: c# asp.net-mvc razorgenerator


【解决方案1】:

这是我如何将枚举转换为选择列表的方法。 Enum.cshtml(一个编辑器模板,带有指向它的 UI 提示):

@model Enum
@Html.DropDownListFor(model => model, Model.ToSelectList(), "Select")

然后是视图中使用的Extension方法:

    /// <summary>
    /// Gets a select list from an enum.
    /// </summary>
    /// <param name="enumObject">The enum object.</param>
    /// <returns></returns>
    public static SelectList ToSelectList(this Enum enumObject)
    {
        List<KeyValuePair<string, string>> selectListItemList = null;
        SelectList selectList = null;

        try
        {
            // Cast the enum values to strings then linq them into a key value pair we can use for the select list.
            selectListItemList = Enum.GetNames(enumObject.GetType()).Cast<string>().Select(item => { return new KeyValuePair<string, string>(item, item.PascalCaseToReadableString()); }).ToList();

            // Remove default value of Enum. This is handled in the editor template with the optionLabel argument.
            selectListItemList.Remove(new KeyValuePair<string, string>("None", "None"));

            // Build the select list from it.
            selectList = new SelectList(selectListItemList, "key", "value", enumObject);

        }
        catch (Exception exception)
        {
            Functions.LogError(exception);
        }

        return selectList;
    }

要将此解决方案重构为复选框列表,您可以简单地从函数中传回键值对并在编辑器模板中循环它们。

希望对你有所帮助。

【讨论】:

    猜你喜欢
    • 2012-09-16
    • 2018-10-18
    • 2017-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-16
    • 2023-02-21
    • 1970-01-01
    相关资源
    最近更新 更多