0

There is an entity that has public List<DayOfWeek> DefaultDaysOfWeek.

I want to bind this list to a set of 7 checkboxes respectively in the view.

I have not found a default way to do something similar to: @Html.EditorFor(model => model.DefaultDaysOfWeek, new { htmlAttributes = new { @class = "form-control" } })

I have also tried the CheckBoxList(For) framework.

I believe the reason none of this has worked is because I cannot access this enumeration in the View.

Max Wenger
  • 19
  • 4
  • Do you mean radio buttons? (checkboxes would allow you to select multiple days, in which case you should consider you own enum with a `[Flags]` attribute) –  Jul 08 '16 at 05:43
  • I would like the user to be able to select multiple days. If I used [Flags] would I have two enums? (`DaysOfWeek` and `DaysOfWeekFlags`) – Max Wenger Jul 08 '16 at 05:50
  • 1
    No, You would just have one - say `[Flags] public enum MyDayOfWeek { Sunday = 1, Monday = 2, Tuesday = 4, .... }` but it really depends on how you are persisting this information. Refer [this answer](http://stackoverflow.com/questions/37990786/how-to-reduce-code-duplication-in-asp-net-mvc-view-when-working-with-flags-enum/37991402#37991402) for an example of binding to an `enum` with a `FlagsAttribute` –  Jul 08 '16 at 06:01
  • So would I then change `public List DefaultDaysOfWeek` to `public List DefaultDaysOfWeek`? – Max Wenger Jul 08 '16 at 06:08
  • No, its just `public MyDayOfWeek DefaultDaysOfWeek { get; set; }` –  Jul 08 '16 at 06:09
  • The other alternatives are to use a view model with a `bool IsSelected` property (along the lines of [this answer](http://stackoverflow.com/questions/29542107/pass-list-of-checkboxes-into-view-and-pull-out-ienumerable/29554416#29554416), or you could just manually create `` (one for each value) –  Jul 08 '16 at 06:12
  • Oh hey, that worked! Thanks so much! – Max Wenger Jul 08 '16 at 06:12

0 Answers0