[ACCEPTED]-Model Bind List of Enum Flags-enum-flags

Accepted answer
Score: 74

In general I avoid using enums when designing 28 my view models because they don't play with 27 ASP.NET MVC's helpers and out of the box 26 model binder. They are perfectly fine in 25 your domain models but for view models you 24 could use other types. So I leave my mapping 23 layer which is responsible to convert back 22 and forth between my domain models and view 21 models to worry about those conversions.

This 20 being said, if for some reason you decide 19 to use enums in this situation you could 18 roll a custom model binder:

public class NotificationDeliveryTypeModelBinder : DefaultModelBinder
{
    public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        var value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
        if (value != null )
        {
            var rawValues = value.RawValue as string[];
            if (rawValues != null)
            {
                NotificationDeliveryType result;
                if (Enum.TryParse<NotificationDeliveryType>(string.Join(",", rawValues), out result))
                {
                    return result;
                }
            }
        }
        return base.BindModel(controllerContext, bindingContext);
    }
}

which will be 17 registered in Application_Start:

ModelBinders.Binders.Add(
    typeof(NotificationDeliveryType), 
    new NotificationDeliveryTypeModelBinder()
);

So far so 16 good. Now the standard stuff:

View model:

[Flags]
public enum NotificationDeliveryType
{
    InSystem = 1,
    Email = 2,
    Text = 4
}

public class MyViewModel
{
    public IEnumerable<NotificationDeliveryType> Notifications { get; set; }
}

Controller:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        var model = new MyViewModel
        {
            Notifications = new[]
            {
                NotificationDeliveryType.Email,
                NotificationDeliveryType.InSystem | NotificationDeliveryType.Text
            }
        };
        return View(model);
    }

    [HttpPost]
    public ActionResult Index(MyViewModel model)
    {
        return View(model);
    }
}

View 15 (~/Views/Home/Index.cshtml):

@model MyViewModel
@using (Html.BeginForm())
{
    <table>
        <thead>
            <tr>
                <th>Notification</th>
            </tr>
        </thead>
        <tbody>
            @Html.EditorFor(x => x.Notifications)
        </tbody>
    </table>
    <button type="submit">OK</button>
}

custom editor template for the NotificationDeliveryType (~/Views/Shared/EditorTemplates/NotificationDeliveryType.cshtml):

@model NotificationDeliveryType

<tr>
    <td>
        @foreach (NotificationDeliveryType item in Enum.GetValues(typeof(NotificationDeliveryType)))
        {
            <label for="@ViewData.TemplateInfo.GetFullHtmlFieldId(item.ToString())">@item</label>
            <input type="checkbox" id="@ViewData.TemplateInfo.GetFullHtmlFieldId(item.ToString())" name="@(ViewData.TemplateInfo.GetFullHtmlFieldName(""))" value="@item" @Html.Raw((Model & item) == item ? "checked=\"checked\"" : "") />
        }
    </td>
</tr>

It's 14 obvious that a software developer (me in 13 this case) writing such code in an editor 12 template shouldn't be very proud of his 11 work. I mean look t it! Even I that wrote 10 this Razor template like 5 minutes ago can 9 no longer understand what it does.

So we 8 refactor this spaghetti code in a reusable 7 custom HTML helper:

public static class HtmlExtensions
{
    public static IHtmlString CheckBoxesForEnumModel<TModel>(this HtmlHelper<TModel> htmlHelper)
    {
        if (!typeof(TModel).IsEnum)
        {
            throw new ArgumentException("this helper can only be used with enums");
        }
        var sb = new StringBuilder();
        foreach (Enum item in Enum.GetValues(typeof(TModel)))
        {
            var ti = htmlHelper.ViewData.TemplateInfo;
            var id = ti.GetFullHtmlFieldId(item.ToString());
            var name = ti.GetFullHtmlFieldName(string.Empty);
            var label = new TagBuilder("label");
            label.Attributes["for"] = id;
            label.SetInnerText(item.ToString());
            sb.AppendLine(label.ToString());

            var checkbox = new TagBuilder("input");
            checkbox.Attributes["id"] = id;
            checkbox.Attributes["name"] = name;
            checkbox.Attributes["type"] = "checkbox";
            checkbox.Attributes["value"] = item.ToString();
            var model = htmlHelper.ViewData.Model as Enum;
            if (model.HasFlag(item))
            {
                checkbox.Attributes["checked"] = "checked";
            }
            sb.AppendLine(checkbox.ToString());
        }

        return new HtmlString(sb.ToString());
    }
}

and we clean the mess 6 in our editor template:

@model NotificationDeliveryType
<tr>
    <td>
        @Html.CheckBoxesForEnumModel()
    </td>
</tr>

which yields the 5 table:

enter image description here

Now obviously it would have been nice 4 if we could provide friendlier labels for 3 those checkboxes. Like for example:

[Flags]
public enum NotificationDeliveryType
{
    [Display(Name = "in da system")]
    InSystem = 1,

    [Display(Name = "@")]
    Email = 2,

    [Display(Name = "txt")]
    Text = 4
}

All we 2 have to do is adapt the HTML helper we wrote 1 earlier:

var field = item.GetType().GetField(item.ToString());
var display = field
    .GetCustomAttributes(typeof(DisplayAttribute), true)
    .FirstOrDefault() as DisplayAttribute;
if (display != null)
{
    label.SetInnerText(display.Name);
}
else
{
    label.SetInnerText(item.ToString());
}

which gives us a better result:

enter image description here

Score: 15

Darin's code was great but I encountered 13 some trouble using it with MVC4.

In the HtmlHelper 12 extension to create the boxes, I kept getting 11 run-time errors that the model was not an 10 enum (specifically, saying System.Object). I 9 reworked the code to take a Lambda expression 8 and cleaned up this issue using the ModelMetadata 7 class:

public static IHtmlString CheckBoxesForEnumFlagsFor<TModel, TEnum>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TEnum>> expression)
{
    ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
    Type enumModelType = metadata.ModelType;

    // Check to make sure this is an enum.
    if (!enumModelType.IsEnum)
    {
        throw new ArgumentException("This helper can only be used with enums. Type used was: " + enumModelType.FullName.ToString() + ".");
    }

    // Create string for Element.
    var sb = new StringBuilder();
    foreach (Enum item in Enum.GetValues(enumModelType))
    {
        if (Convert.ToInt32(item) != 0)
        {
            var ti = htmlHelper.ViewData.TemplateInfo;
            var id = ti.GetFullHtmlFieldId(item.ToString());
            var name = ti.GetFullHtmlFieldName(string.Empty);
            var label = new TagBuilder("label");
            label.Attributes["for"] = id;
            var field = item.GetType().GetField(item.ToString());

            // Add checkbox.
            var checkbox = new TagBuilder("input");
            checkbox.Attributes["id"] = id;
            checkbox.Attributes["name"] = name;
            checkbox.Attributes["type"] = "checkbox";
            checkbox.Attributes["value"] = item.ToString();
            var model = htmlHelper.ViewData.Model as Enum;
            if (model.HasFlag(item))
            {
                checkbox.Attributes["checked"] = "checked";
            }
            sb.AppendLine(checkbox.ToString());

            // Check to see if DisplayName attribute has been set for item.
            var displayName = field.GetCustomAttributes(typeof(DisplayNameAttribute), true)
                .FirstOrDefault() as DisplayNameAttribute;
            if (displayName != null)
            {
                // Display name specified.  Use it.
                label.SetInnerText(displayName.DisplayName);
            }
            else
            {
                // Check to see if Display attribute has been set for item.
                var display = field.GetCustomAttributes(typeof(DisplayAttribute), true)
                    .FirstOrDefault() as DisplayAttribute;
                if (display != null)
                {
                    label.SetInnerText(display.Name);
                }
                else
                {
                    label.SetInnerText(item.ToString());
                }
            }
            sb.AppendLine(label.ToString());

            // Add line break.
            sb.AppendLine("<br />");
        }                
    }

    return new HtmlString(sb.ToString());
}

I also extended the model binder so 6 it works with any generic enum type.

public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
    // Fetch value to bind.
    var value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
    if (value != null)
    {
        // Get type of value.
        Type valueType = bindingContext.ModelType;

        var rawValues = value.RawValue as string[];
        if (rawValues != null)
        {
            // Create instance of result object.
            var result = (Enum)Activator.CreateInstance(valueType);

            try
            {
                // Parse.
                result = (Enum)Enum.Parse(valueType, string.Join(",", rawValues));
                return result;
            }
            catch
            {
                return base.BindModel(controllerContext, bindingContext);
            }
        }
    }
    return base.BindModel(controllerContext, bindingContext);
}

You 5 still need to register each enum type in 4 Application_Start but at least this eliminates 3 the need for separate binder classes. You 2 can register it using:

ModelBinders.Binders.Add(typeof(MyEnumType), new EnumFlagsModelBinder());

I posted my code on 1 Github at https://github.com/Bitmapped/MvcEnumFlags.

Score: 7

You can try the MVC Enum Flags package (available via 5 nuget). It automatically skips zero-valued enum 4 choices, which is a nice touch.

[The following is from the Documentation and its comments; see there if this is not binding properly for you]

After installation, add 3 the following to Global.asax.cs\Application_Start:

ModelBinders.Binders.Add(typeof(MyEnumType), new EnumFlagsModelBinder());

Then 2 in the view, put @using MvcEnumFlags up top and @Html.CheckBoxesForEnumFlagsFor(model => model.MyEnumTypeProperty) for the actual 1 code.

Score: 2

I use approach described in MVVM Framework.

 enum ActiveFlags
{
    None = 0,
    Active = 1,
    Inactive = 2,
}

class ActiveFlagInfo : EnumInfo<ActiveFlags>
{
    public ActiveFlagInfo(ActiveFlags value)
        : base(value)
    {
        // here you can localize or set user friendly name of the enum value
        if (value == ActiveFlags.Active)
            this.Name = "Active";
        else if (value == ActiveFlags.Inactive)
            this.Name = "Inactive";
        else if (value == ActiveFlags.None)
            this.Name = "(not set)";
    }
}

   // Usage of ActiveFlagInfo class:
   // you can use collection of ActiveFlagInfo for binding in your own view models
   // also you can use this ActiveFlagInfo as property for your  classes to wrap enum properties

   IEnumerable<ActiveFlagInfo> activeFlags = ActiveFlagInfo.GetEnumInfos(e => 
                    e == ActiveFlags.None ? null : new ActiveFlagInfo(e));

0

Score: 1

Bitmapped, you have asked important questions 4 and i can suggest following solution: you 3 should override BindProperty method of your 2 ModelBinder and next need to override model 1 property value:

protected override void BindProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, System.ComponentModel.PropertyDescriptor propertyDescriptor)
{
    if (propertyDescriptor.PropertyType.IsEnum && propertyDescriptor.PropertyType.GetCustomAttributes(typeof(FlagsAttribute), false).Any())
    {
        var value = bindingContext.ValueProvider.GetValue(propertyDescriptor.Name);
        if (value != null)
        {
            // Get type of value.
            var rawValues = value.RawValue as string[];
            if (rawValues != null)
            {
                // Create instance of result object.
                var result = (Enum)Activator.CreateInstance(propertyDescriptor.PropertyType);
                try
                {
                    // Try parse enum
                    result = (Enum)Enum.Parse(propertyDescriptor.PropertyType, string.Join(",", rawValues));
                    // Override property with flags value
                    propertyDescriptor.SetValue(bindingContext.Model, result);
                    return;
                }
                catch
                {                               
                }
            }
        }
        base.BindProperty(controllerContext, bindingContext, propertyDescriptor);
    }
    else
        base.BindProperty(controllerContext, bindingContext, propertyDescriptor);
}
Score: 1

Using Darin and bitmapped code, i write 13 the answer, but didn't worked for me, so 12 first i fixed nullable stuff, then still 11 i had bining issue, found out that there's 10 something wrong with the html, so i loos 9 faith on this answer, search for another, that 8 i found something in a forum of my country, that 7 used the same code as here, but with a very 6 little change, so i merge that with my code, and 5 it all went well, my project use nullable, so 4 i don't know how it gonna work on other 3 places, may need a little fix, but i tried 2 to think about nullable and model being 1 the enum itself.

public static class Extensions
{
    public static IHtmlString CheckBoxesForEnumFlagsFor<TModel, TEnum>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TEnum>> expression)
    {
        ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
        Type enumModelType = metadata.ModelType;

        var isEnum = enumModelType.IsEnum;
        var isNullableEnum = enumModelType.IsGenericType &&
                             enumModelType.GetGenericTypeDefinition() == typeof (Nullable<>) &&
                             enumModelType.GenericTypeArguments[0].IsEnum;

        // Check to make sure this is an enum.
        if (!isEnum && !isNullableEnum)
        {
            throw new ArgumentException("This helper can only be used with enums. Type used was: " + enumModelType.FullName.ToString() + ".");
        }

        // Create string for Element.
        var sb = new StringBuilder();

        Type enumType = null;
        if (isEnum)
        {
            enumType = enumModelType;
        }
        else if (isNullableEnum)
        {
            enumType = enumModelType.GenericTypeArguments[0];
        }

        foreach (Enum item in Enum.GetValues(enumType))
        {
            if (Convert.ToInt32(item) != 0)
            {
                var ti = htmlHelper.ViewData.TemplateInfo;
                var id = ti.GetFullHtmlFieldId(item.ToString());

                //Derive property name for checkbox name
                var body = expression.Body as MemberExpression;
                var propertyName = body.Member.Name;
                var name = ti.GetFullHtmlFieldName(propertyName);

                //Get currently select values from the ViewData model
                //TEnum selectedValues = expression.Compile().Invoke(htmlHelper.ViewData.Model);

                var label = new TagBuilder("label");
                label.Attributes["for"] = id;
                label.Attributes["style"] = "display: inline-block;";
                var field = item.GetType().GetField(item.ToString());

                // Add checkbox.
                var checkbox = new TagBuilder("input");
                checkbox.Attributes["id"] = id;
                checkbox.Attributes["name"] = name;
                checkbox.Attributes["type"] = "checkbox";
                checkbox.Attributes["value"] = item.ToString();

                var model = (metadata.Model as Enum);

                //var model = htmlHelper.ViewData.Model as Enum; //Old Code
                if (model != null && model.HasFlag(item))
                {
                    checkbox.Attributes["checked"] = "checked";
                }
                sb.AppendLine(checkbox.ToString());

                // Check to see if DisplayName attribute has been set for item.
                var displayName = field.GetCustomAttributes(typeof(DisplayNameAttribute), true)
                    .FirstOrDefault() as DisplayNameAttribute;
                if (displayName != null)
                {
                    // Display name specified.  Use it.
                    label.SetInnerText(displayName.DisplayName);
                }
                else
                {
                    // Check to see if Display attribute has been set for item.
                    var display = field.GetCustomAttributes(typeof(DisplayAttribute), true)
                        .FirstOrDefault() as DisplayAttribute;
                    if (display != null)
                    {
                        label.SetInnerText(display.Name);
                    }
                    else
                    {
                        label.SetInnerText(item.ToString());
                    }
                }
                sb.AppendLine(label.ToString());

                // Add line break.
                sb.AppendLine("<br />");
            }
        }

        return new HtmlString(sb.ToString());
    }
}

 

public class FlagEnumerationModelBinder : DefaultModelBinder
{
    public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        if (bindingContext == null) throw new ArgumentNullException("bindingContext");

        if (bindingContext.ValueProvider.ContainsPrefix(bindingContext.ModelName))
        {
            var values = GetValue<string[]>(bindingContext, bindingContext.ModelName);

            if (values.Length > 1 && (bindingContext.ModelType.IsEnum && bindingContext.ModelType.IsDefined(typeof(FlagsAttribute), false)))
            {
                long byteValue = 0;
                foreach (var value in values.Where(v => Enum.IsDefined(bindingContext.ModelType, v)))
                {
                    byteValue |= (int)Enum.Parse(bindingContext.ModelType, value);
                }

                return Enum.Parse(bindingContext.ModelType, byteValue.ToString());
            }
            else
            {
                return base.BindModel(controllerContext, bindingContext);
            }
        }

        return base.BindModel(controllerContext, bindingContext);
    }

    private static T GetValue<T>(ModelBindingContext bindingContext, string key)
    {
        if (bindingContext.ValueProvider.ContainsPrefix(key))
        {
            ValueProviderResult valueResult = bindingContext.ValueProvider.GetValue(key);
            if (valueResult != null)
            {
                bindingContext.ModelState.SetModelValue(key, valueResult);
                return (T)valueResult.ConvertTo(typeof(T));
            }
        }
        return default(T);
    }
}

 

ModelBinders.Binders.Add(
            typeof (SellTypes),
            new FlagEnumerationModelBinder()
            );
ModelBinders.Binders.Add(
            typeof(SellTypes?),
            new FlagEnumerationModelBinder()
            );

More Related questions