Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>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 <a href="http://msdn.microsoft.com/en-us/library/cc138362.aspx" rel="nofollow noreferrer">enum flags documentation</a>.</p> <p><strong>DayOfWeek enum using bit flags:</strong></p> <pre><code>[Flags]//&lt;-- Note the Flags attribute public enum DayOfWeek { Monday = 1, Tuesday = 2, Wednesday = 4, Thursday = 8, Friday = 16, Saturday = 32, Sunday = 64, } </code></pre> <p><strong>Model:</strong></p> <pre><code>public class Document { public string Name { get; set;} //Note that WeekDays is no longer a collection. public DayOfWeek WeekDays { get; set; } } </code></pre> <p><strong>View:</strong></p> <pre><code>&lt;% foreach(DayOfWeek dayOfWeek in Enum.GetValues(typeof(DayOfWeek))) { %&gt; &lt;label&gt; &lt;!-- The HasFlag stuff is optional and is just there to show how it would be populated if you're doing a `GET` request. --&gt; &lt;input type="checkbox" name="WeekDays[]" value="&lt;%= (int)dayOfWeek%&gt;" &lt;%= Model.WeekDays.HasFlag(dayOfWeek) ? "checked='checked'" : "" %&gt;" /&gt; &lt;%= dayOfWeek %&gt; &lt;/label&gt; &lt;% } %&gt; </code></pre> <p><strong>Controller:</strong></p> <pre><code>[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&lt;DayOfWeek&gt;.ParseToEnumFlag(Request.Form, "WeekDays[]"); ... } </code></pre> <p><strong>Quick helper for ParseToEnumFlag:</strong></p> <pre><code>public static class Enum&lt;T&gt; { 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&lt;int&gt;(',').Sum()); } } </code></pre> <p><strong>Background:</strong> The reason the enum flags values are in a geometric series (<a href="http://en.wikipedia.org/wiki/Geometric_series" rel="nofollow noreferrer">1,2,4,8...</a>) is so that, when the values are added together, there is only one possible combination. For example, we would know that <code>31</code> could only be Mon, Tue, Wed, Thur and Fri (1 + 2 + 4 + 8 + 16).</p> <p><strong>Update - 3rd September 2012</strong></p> <p>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 <a href="https://stackoverflow.com/users/1242061/escist">@escist</a> for the headsup.</p> <pre><code>public static IEnumerable&lt;T&gt; ToIEnumerable&lt;T&gt;(this string source, char delimiter) { return source.Split(new char[] { delimiter }, StringSplitOptions.RemoveEmptyEntries).Select(x =&gt; (T)Convert.ChangeType(x, typeof(T))); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload