Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
334 views
in Technique[技术] by (71.8m points)

c# - MVC 2 - Passing enum to CheckBoxFor

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)));
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...