6

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

I have an enum type

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

and my Model is

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)

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.

4
  • Besides the lack of a ViewModel, I don't see a better way. HTML itself will not handle flag checkboxes which leaves only 2 options: What you did (seperated checkboxes handled server-side) and using javascript that manages checkboxes (not mapped) and a hidden input (mapped) adding the selected flag values and setting that input. Out of the 2 options, yours is much cleaner. The only advantage to the javascript one would be the reduced POST data. Commented Aug 6, 2013 at 18:25
  • @Pluc Thanks for your reply. But then would that mean that if i were to add more modes to my EnumType then i would have to add properties as well. Commented Aug 6, 2013 at 19:06
  • Yes it would. I suppose you could also hack something that iterates each options in your enum and generates the checkboxes, and then gather the responce from the form result (not binded to your model) to add them up and save the result in your model. It wouldnt be as clean, but it would scale with your enum changes. But in all honesty, if you plan on adding enum choices, you probably want to choose the order they display (view) and when you touch your view, it's not all that hard to change it in your View Model Commented Aug 7, 2013 at 12:44
  • Related: stackoverflow.com/questions/9264927/… Commented Aug 11, 2014 at 21:30

1 Answer 1

6

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.