为的CheckBoxList枚举类型MVC剃刀 [英] CheckBoxList for Enum types MVC Razor

查看:243
本文介绍了为的CheckBoxList枚举类型MVC剃刀的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的C#.NET MVC应用程序我想显示复选框列表枚举类型。

In my c#.net MVC application i would like to display checkbox list for Enum types.

我有一个枚举类型

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

和我的模型

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

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

In my view i need two checkbox one for Read and the other for Edit So i tried

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

但是,这给我的错误
模板只能与现场访问,访问属性,一维数组的索引,或单参数自定义索引前pressions使用。

But this give me error "Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions."

我的工作解决此问题,通过增加两个属性,我的模型

I worked around this problem by adding two more properties to my model

 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;}
         }

   }

,然后让我的看法

and then making my view

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

我知道,我已经接近它的方式不正确,应该有更好的方式来实现这一目标。
可能有人请告诉我这一点。

I know that the way i have approached it is not correct and there should be a better way to achieve this. Could someone please advise me on this.

推荐答案

下面是我如何解决这转化成枚举选择列表。 Enum.cshtml(一名编辑模板,用UI提示指向它):

Here is how I tackled this to transforms Enums into Select Lists. Enum.cshtml (an Editor Template, with a UI Hint to point to it):

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

然后在视图中使用的扩展方法:

Then the Extension method used in the view:

    /// <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;
    }

要重构这个解决方案为复选框列表,你可以在编辑器模板通过他们简单地传回从功能和环路的键值对。

To refactor this solution into Check Box Lists, you could simply pass back the Key Value Pairs from the function and loop through them in the editor template.

我希望这是有一定的帮助。

I hope this is of some help.

这篇关于为的CheckBoxList枚举类型MVC剃刀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆