Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Month()
- {
- public int ID { get; set; }
- public string Name { get; set; }
- public virtual ICollection<Week> Weeks { get; set; }
- }
- public class Week()
- {
- public int ID { get; set; }
- public int MonthID { get; set; }
- public String Name { get; set; }
- public virtual ICollection<Day> Days { get; set; }
- }
- public class Day()
- {
- public int ID { get; set; }
- public String Name { get; set; }
- }
- public class EditMonthViewModel()
- {
- public Month Month { get; set; }
- public List<Week> Weeks { get; set; }
- public List<Day> AllDays { get; set; }
- }
- @model myProject.ViewModels.EditMonthViewModel
- //...
- @using (Html.BeginForm())
- {
- //Edit Month Stuff...
- @for(int i = 0; i < Model.Weeks.Count(); i++)
- {
- <h2>@Model.Weeks[i].Name</h2>
- @Html.EditorFor(model => Model.Weeks[i].Name)
- //loop through all possible days
- //Select only days that are assigned to Week[i]
- @for(int d = 0; d < Model.AllDays.Count(); d ++)
- {
- //This is the focus of this question.
- //How do you bind the data here?
- <input type="checkbox"
- name="I have no idea"
- @Html.Raw(Model.Weeks[i].Days.Contains(Model.AllDays[d]) ? "checked" : "") />
- }
- }
- }
- public ActionResult Edit(int id)
- {
- var viewModel = new EditMonthViewModel();
- viewModel.Month = db.Months.Find(id);
- viewModel.Weeks = db.Weeks.Where(w => w.MonthID == id).ToList();
- viewModel.AllDays = db.Days.ToList();
- }
- [HttpPost]
- public ActionResult Edit(EditMonthViewModel viewModel)
- {
- var monthToUpdate = db.Months.Find(viewModel.Month.ID);
- //...
- if(viewModel.Weeks != null)
- {
- foreach (var week in viewModel.Weeks)
- {
- var weekToUpdate = monthToUpdate.Weeks.Single(w => w.ID == week.ID);
- //...
- /*So, I have a collection of weeks that I can grab,
- but how do I know what was selected? My viewModel only has a
- list of AllDays, not the days selected for Week[i]
- */
- }
- }
- public class DayModel
- {
- public int WeekId { get; set; }
- public int DayId { get; set; }
- public bool IsIncluded { get; set; }
- }
- @{
- var modelIdx = 0;
- }
- // ...
- <input type="hidden" name="days[@modelIdx].WeekId" value="@Model.Weeks[i].Id" />
- <input type="hidden" name="days[@modelIdx].DayId" value="@Model.AllDays[d].Id" />
- <input type="checkbox" name="days[@modelIdx].IsIncluded" value="@(Model.Weeks[i].Days.Contains(Model.AllDays[d]) ? "checked" : "")" />
- @{ modelIdx++; }
- [HttpPost]
- public ActionResult Edit(IEnumerable<DayModel> days)
- {
- //...
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement