I've got a view that needs some checkboxes describing days of the week. I want to return back to the controller which boxes are selected. I found some help from various Google searches, etc, and here's what I have.
Section 1:
In the Controller:
vm.AllDays = Enum.GetValues(typeof(DayOfWeek)).Cast<DayOfWeek>().ToList();
In the View:
@foreach (var day in Model.AllDays) {
<input type="checkbox" name="days" value="@day"/> <label>@day</label><br />
}
And then in my post handler:
[HttpPost]
public ActionResult _AddEdit(SubscriptionDetailsModel vm, IEnumerable<string> days) {
//etc, etc, etc
}
This works, I get the selected days coming in on the days parameter. However, I got curious and changed the 'name' attribute on the checkboxes to the field in my model that I ultimately want these values to go to. In summary, my model for the view contains an object that has a List in it. That List is the collection I would like the selected values to end up in. But here's the relevant code.
Section 2
Model snippets:
public class SubscriptionDetailsModel {
public CronTabModel CrontabModel { get; set; }
//...
}
public class CronTabModel {
public List<DayOfWeek> On { get; set; }
}
Controller:
[HttpPost]
public ActionResult _AddEdit(SubscriptionDetailsModel vm) {
//etc, etc, etc
}
And, of course, view:
@foreach (var day in Model.AllDays) {
<input type="checkbox" name="On" value="@day"/> <label>@day</label><br />
}
Edit: The reason I was seeing confusing results is due to a typo elsewhere in my View.
Technically, the code in Section 1 is usable for my situation, I can do some string parsing, etc. Is it possible to make the Checkbox group populate from one collection (Model.AllDays) and return in a second collection (Model.CronTabModel.On)?