如何重新present一个月复选框在MVC模型 [英] How to represent a month of checkboxes in an MVC model

查看:118
本文介绍了如何重新present一个月复选框在MVC模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不能设法让我围绕着如何使用MVC创建下表头,并成功地将其绑定到一个模型:

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:

修改:这不是一个月,但对于一个任意4周周期

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
}

和我可以成功地呈现一个视图(未绑定到模型)。我意识到我需要的地方对我的投入使用@ html.CheckBoxFor。

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.

下面是我的HTML的一个粗略的副本的观点:

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>
}

我怎么会成功发布它的复选框被选中?请问我的ViewModel有意义吗?

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

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;}          
}

然后在视图

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>
}

附注:我不认为你真的需要你自己在这里枚举 - 你可以只使用星期几枚举,例如天。加入(新DayVM(){天= DayOfWeek.Sunday}); 。还要注意我还没有为的 Day属性,因为你可以很容易地确定其在集合中的索引输入。事实上,属性可能不会在所有的,如果你手工绘制的表格标题行是必需的。

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.

这篇关于如何重新present一个月复选框在MVC模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆