CheckBoxList for Enum types MVC Razor
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.
ammu
Updated on June 22, 2022Comments
-
ammu almost 2 years
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.