How to represent a month of checkboxes in an MVC m

2020-01-29 02:08发布

问题:

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?

回答1:

I suggest you change you view model(s) to

public class DayVM
{
  public Day Day { get; set; }
  public bool IsSelected { get; set; }
}
public class WeekVM
{
  public WeekVM()
  {
    Days = new List<DayVM>();
    Days.Add(new DayVM() { Day = Day.Sunday });
    Days.Add(new DayVM() { Day = Day.Monday });
    .. etc
  }
  public List<DayVM> Days { get; set; }
}
public class ScheduleViewModel
{
  public ScheduleViewModel()
  {
    Weeks = new List<WeekVM>();
    Weeks.Add(new WeekVM());
    .... etc
  }
  public int PatientId { get; set; }          
  public List<WeekVM> Weeks { get; set;}          
}

Then in the view

for(int i = 0; i < Model.Weeks.Count; i++)
{
  <tr>
    <td>Week @i</td>
    for(int j = 0; j < Model.Weeks[i].Days.Count; j++)
    {
      <td>
        @Html.CheckBoxFor(m => m.Weeks[i].Days[j].IsSelected)
      </td>
    }
  </tr>
}

Side note: I don't think you really need you own enum here - your could just use the DayOfWeek enumeration, for example Days.Add(new DayVM() { Day = DayOfWeek.Sunday });. Note also I have not included an input for the Day property since you could easily determine that from its index in the collection. In fact the Day property may not be required at all if you manually render the table header row.