0

Bootstrap can show a group of inline checkboxes like this:

<label class="checkbox-inline">
  <input type="checkbox" id="inlineCheckbox1" value="option1"> 1
</label>
<label class="checkbox-inline">
  <input type="checkbox" id="inlineCheckbox2" value="option2"> 2
</label>
<label class="checkbox-inline">
  <input type="checkbox" id="inlineCheckbox3" value="option3"> 3
</label>

Now suppose that these checkboxes must come from an enum in my viewmodel. So for example I have HolidayModel.RoomPrefs which is an enum like this:

[Flags]
enum ReservationPreferences {
  Room = 2,
  Breakfast = 4,
  Lunch = 8,
  Dinner = 16
}

I want to render this enum as a group of inline checkboxes (i.e. the markup above).

I can do so manually, but I rather use a helper or editor template, or something else generic, because the HolidayModel viewmodel has other enums and so I want to reuse the code.

Does Razor have something out of the box to support this?

h bob
  • 3,610
  • 3
  • 35
  • 51
  • possible duplicate of [Model Bind List of Enum Flags](http://stackoverflow.com/questions/9264927/model-bind-list-of-enum-flags) (not with Bootstrap checkboxes but pretty easy to adapt) – Adriano Repetti Sep 26 '14 at 14:11
  • @AdrianoRepetti Wow, interesting but very complicated. Not for Bootstrap as you said. I wonder if there is an easier/better way, as that answer was written many versions ago (of MVC). Also the model binder has knowledge of the concrete model, so it's not exactly generic- was probably the best solution at the time. – h bob Sep 26 '14 at 16:44

1 Answers1

0

At this time, MVC has no "out-of-the-box" way to do this. You need to use a combination of custom model binding, and/or editor templates and/or something else.

I've removed enums from my viewmodel and replaced with a series of ugly bools. Does the job.

h bob
  • 3,610
  • 3
  • 35
  • 51