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.
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):
Then the Extension method used in the view:
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.