I can't manage to get my head around how to use MVC to create the following table, and successfully bind it to a model:
I basically need to keep track of which days of the month an event needs to happen. Here is my attempt at a model:
EDIT: This isn't for a month, but for a arbitrary 4 week cycle
public class ScheduleViewModel
{
public int PatientId { get; set; }
public List<Schedule> Schedules { get; set;}
}
public class Schedule {
public int Week { get;set;}
public Day Day { get;set;}
public bool IsSelected { get;set;}
}
public enum Day
{
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday,
Sunday
}
And I can render a view successfully (that isn't bound to the model). I realise that I would need to use @html.CheckBoxFor in place on my inputs.
Here is a rough copy of my html for the view:
@model WebApplication10.Models.ScheduleViewModel
@using (Html.BeginForm())
{
<table class="table table-striped">
<thead>
<tr>
<th></th>
@{
foreach (Day t in Enum.GetValues(typeof(Day)))
{
<th>@t</th>
}
}
</tr>
</thead>
<tbody>
@{
for (int i = 1; i <= 4; i++)
{
<tr>
<td>Week @i</td>
@foreach (Day t in Enum.GetValues(typeof(Day)))
{
<td><input type="checkbox" /></td>
}
</tr>
}
}
</tbody>
</table>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
}
How would I successfully post which check-boxes have been selected? Does my ViewModel make sense?
I suggest you change you view model(s) to
Then in the view
Side note: I don't think you really need you own enum here - your could just use the
DayOfWeek
enumeration, for exampleDays.Add(new DayVM() { Day = DayOfWeek.Sunday });
. Note also I have not included an input for theDay
property since you could easily determine that from its index in the collection. In fact theDay
property may not be required at all if you manually render the table header row.