8

Let's assume we have a model:

public class Document
{
    public string Name { get; set;}
    public List<DayOfWeek> WeekDays { get; set; }
}

Is it possible to render checkboxes that represent days of week for that model? I've searched the internet but did not find any solution.

I mean it works whith CheckBoxFor(model=> model.SomeProperty) but it does not work if SomeProperty is List<DayOfWeek>. DayOfWeek here is an enumeration.

Thanks in advance.

Michael Blackburn
  • 3,161
  • 1
  • 25
  • 18
ilkin
  • 2,862
  • 3
  • 20
  • 20

5 Answers5

19

This would require a change to your DayOfWeek enum, but I favour doing it as a flag (less messy, only one value, etc). Interestingly enough, Microsoft also use days of the week in their enum flags documentation.

DayOfWeek enum using bit flags:

[Flags]//<-- Note the Flags attribute
public enum DayOfWeek
{
  Monday = 1,
  Tuesday = 2,
  Wednesday = 4,
  Thursday = 8,
  Friday = 16,
  Saturday = 32,
  Sunday = 64,
}

Model:

public class Document
{
  public string Name { get; set;}

  //Note that WeekDays is no longer a collection.
  public DayOfWeek WeekDays { get; set; }
}

View:

<% foreach(DayOfWeek dayOfWeek in Enum.GetValues(typeof(DayOfWeek))) { %>
  <label>
    <!-- The HasFlag stuff is optional and is just there to show how it would be populated if you're doing a `GET` request. -->
    <input type="checkbox" name="WeekDays[]" value="<%= (int)dayOfWeek%>" <%= Model.WeekDays.HasFlag(dayOfWeek) ? "checked='checked'" : "" %>" />
    <%= dayOfWeek %>
  </label>
<% } %>

Controller:

[HttpPost]
public ActionResult MyPostedPage(MyModel model)
{
  //I moved the logic for setting this into a helper because this could be re-used elsewhere.
  model.WeekDays = Enum<DayOfWeek>.ParseToEnumFlag(Request.Form, "WeekDays[]");
  ...
}

Quick helper for ParseToEnumFlag:

public static class Enum<T>
{
  public static T ParseToEnumFlag(NameValueCollection source, string formKey)
  {
    //MVC 'helpfully' parses the checkbox into a comma-delimited list. We pull that out and sum the values after parsing it back into the enum.
    return (T)Enum.ToObject(typeof(T), source.Get(formKey).ToIEnumerable<int>(',').Sum());
  }
}

Background: The reason the enum flags values are in a geometric series (1,2,4,8...) is so that, when the values are added together, there is only one possible combination. For example, we would know that 31 could only be Mon, Tue, Wed, Thur and Fri (1 + 2 + 4 + 8 + 16).

Update - 3rd September 2012

It seems I missed out the ToIEnumerable() which is an extension in our source code. It takes a delimited string and casts it into an IEnumerable so is perfect for comma delimited numbers. Thanks to @escist for the headsup.

public static IEnumerable<T> ToIEnumerable<T>(this string source, char delimiter)
{
  return source.Split(new char[] { delimiter }, StringSplitOptions.RemoveEmptyEntries).Select(x => (T)Convert.ChangeType(x, typeof(T)));
}
Community
  • 1
  • 1
Dan Atkinson
  • 11,391
  • 14
  • 81
  • 114
  • I could not compile this. The snippet `source.Get(formKey).ToIEnumerable(',').Sum())` gives a compilation error – escist Sep 03 '12 at 08:07
  • @DanAtkinson I love this answer, but I am having a bit of trouble converting the view to Razor View (I am working with ASP.NET MVC 4). Could you possibly write the Razor View version of this or give links on how I should convert it? Thanks. – John Odom Sep 17 '14 at 17:04
13

You can enumerate on the values of the enum and manually create the checkboxes. Using the same name for each checkbox will submit them as an array in the ActionMethod.

<% foreach(var value in Enum.GetValues(typeof(DayOfWeek))) { %>
     <% var name = Enum.GetName(typeof(DayOfWeek), value); %>
     <label for="dayofweek<%=value %>"><%=name %></label>
     <input type="checkbox" id="dayofweek<%=value %>" name="dayofweek" value="<%=value %>" />
<% } %>

Your action method would be something like:

public ActionResult Save(DayOfWeek[] dayofweek)
{
     // Do Stuff
}
bmancini
  • 2,028
  • 15
  • 21
2

Based on Dan Atkinson's (great) answer, I'd made a little bit of shortcuts here and there. My suggestion would be:

The same enum with [Flags] and model.

For View, I wouldn't change type to int but keep checkbox values as string:

<input type="checkbox" name="WeekDays[]" 
       value="<%= dayOfWeek %>" 
       <%= Model.WeekDays.HasFlag(dayOfWeek) ? "checked='checked'" : "" %>" />

Which makes the Controller much simpler:

[HttpPost]
public ActionResult MyPostedPage(MyModel model)
{
    string days = Request.Form.get("WeekDays[]");
    if (days == null) {
        model.WeekDays = 0;  // Depending whether you allow neither day to be selected
                             // you can handle this differently
    } else {
        model.WeekDays = (WeekDays)Enum.Parse(typeof(WeekDays), days);
    }

    ...
 }

Note, the code does not use extensions at all.

Cheers,

Jacek
  • 1,048
  • 15
  • 21
1

Note: I had an issue implementing Dan's answer using HasFlag() in the view when using dayOfWeek declared as a var. I had to declare it as the DayOfWeek Enum.

View:

<% foreach(DayOfWeek dayOfWeek in Enum.GetValues(typeof(DayOfWeek))) { %>
  <label>
    <!-- The HasFlag stuff is optional and is just there to show how it would be populated if you're doing a `GET` request. -->
    <input type="checkbox" name="WeekDays[]" value="<%= (int)dayOfWeek%>" <%= Model.WeekDays.HasFlag(dayOfWeek) ? "checked='checked'" : "" %>" />
    <%= dayOfWeek %>
  </label>
<% } %>

I also had and issue with the ToIEnumerable function in helper as there is no function with that name (at least for me).

Picrofo Software
  • 5,475
  • 3
  • 23
  • 37
Profex
  • 1,370
  • 8
  • 20
1

You could by creating your own custom template which knows how to take the enum list and transform it into checkboxes. Then you'd have to adjust the model binder to handle binding the enum based on the checked value. Since your question lacks details I'm not sure what the use case for this model is.

This is how you'd inline it in your view:

<% foreach(var dayOfWeek in Model.WeekDays) { %>
    <%= dayOfWeek.ToString() %><%= Html.CheckBox(dayOfWeek.ToString()) %>
<% } %>
Dan Atkinson
  • 11,391
  • 14
  • 81
  • 114
John Farrell
  • 24,673
  • 10
  • 77
  • 110
  • Thanks for the answer. I just want to use the DayOfWeek enumerator which contains days of week. Every Document object contais a list of days. And i want to create checkboxes for this class so it would be possible to choose multiple days. For example CheckBoxFor(model=> model.SomeProperty) works perfect but not if SomeProperty is List<> – ilkin Nov 16 '10 at 13:39