Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.64 KB | None | 0 0
  1. public class Month()
  2. {
  3. public int ID { get; set; }
  4. public string Name { get; set; }
  5. public virtual ICollection<Week> Weeks { get; set; }
  6. }
  7.  
  8. public class Week()
  9. {
  10. public int ID { get; set; }
  11. public int MonthID { get; set; }
  12. public String Name { get; set; }
  13. public virtual ICollection<Day> Days { get; set; }
  14. }
  15.  
  16. public class Day()
  17. {
  18. public int ID { get; set; }
  19. public String Name { get; set; }
  20. }
  21.  
  22. public class EditMonthViewModel()
  23. {
  24. public Month Month { get; set; }
  25. public List<Week> Weeks { get; set; }
  26. public List<Day> AllDays { get; set; }
  27. }
  28.  
  29. @model myProject.ViewModels.EditMonthViewModel
  30.  
  31. //...
  32. @using (Html.BeginForm())
  33. {
  34. //Edit Month Stuff...
  35.  
  36. @for(int i = 0; i < Model.Weeks.Count(); i++)
  37. {
  38. <h2>@Model.Weeks[i].Name</h2>
  39. @Html.EditorFor(model => Model.Weeks[i].Name)
  40.  
  41. //loop through all possible days
  42. //Select only days that are assigned to Week[i]
  43. @for(int d = 0; d < Model.AllDays.Count(); d ++)
  44. {
  45. //This is the focus of this question.
  46. //How do you bind the data here?
  47. <input type="checkbox"
  48. name="I have no idea"
  49. @Html.Raw(Model.Weeks[i].Days.Contains(Model.AllDays[d]) ? "checked" : "") />
  50. }
  51. }
  52. }
  53.  
  54. public ActionResult Edit(int id)
  55. {
  56. var viewModel = new EditMonthViewModel();
  57. viewModel.Month = db.Months.Find(id);
  58. viewModel.Weeks = db.Weeks.Where(w => w.MonthID == id).ToList();
  59.  
  60. viewModel.AllDays = db.Days.ToList();
  61. }
  62.  
  63. [HttpPost]
  64. public ActionResult Edit(EditMonthViewModel viewModel)
  65. {
  66. var monthToUpdate = db.Months.Find(viewModel.Month.ID);
  67. //...
  68.  
  69. if(viewModel.Weeks != null)
  70. {
  71. foreach (var week in viewModel.Weeks)
  72. {
  73. var weekToUpdate = monthToUpdate.Weeks.Single(w => w.ID == week.ID);
  74. //...
  75.  
  76. /*So, I have a collection of weeks that I can grab,
  77. but how do I know what was selected? My viewModel only has a
  78. list of AllDays, not the days selected for Week[i]
  79. */
  80. }
  81. }
  82.  
  83. public class DayModel
  84. {
  85. public int WeekId { get; set; }
  86. public int DayId { get; set; }
  87. public bool IsIncluded { get; set; }
  88. }
  89.  
  90. @{
  91. var modelIdx = 0;
  92. }
  93.  
  94. // ...
  95.  
  96. <input type="hidden" name="days[@modelIdx].WeekId" value="@Model.Weeks[i].Id" />
  97. <input type="hidden" name="days[@modelIdx].DayId" value="@Model.AllDays[d].Id" />
  98. <input type="checkbox" name="days[@modelIdx].IsIncluded" value="@(Model.Weeks[i].Days.Contains(Model.AllDays[d]) ? "checked" : "")" />
  99. @{ modelIdx++; }
  100.  
  101. [HttpPost]
  102. public ActionResult Edit(IEnumerable<DayModel> days)
  103. {
  104. //...
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement