如何在 MVC 模型中表示一个月的复选框 [英] How to represent a month of checkboxes in an MVC model

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

问题描述

我无法理解如何使用 MVC 创建下表并将其成功绑定到模型:

我基本上需要跟踪事件需要在每月的哪几天发生.这是我对模型的尝试:

编辑:这不是一个月,而是任意 4 周周期

公共类 ScheduleViewModel{公共 int PatientId { 获取;放;}公共列表<时间表>时间表{得到;放;}}公开课时间表{公共整数周{获取;设置;}公共日 { get;set;}public bool IsSelected { get;set;}}公共枚举日{周一,周二,周三,周四,星期五,周六,星期日}

而且我可以成功渲染视图(未绑定到模型).我意识到我需要在我的输入中使用 @html.CheckBoxFor.

这是我用于视图的 html 的粗略副本:

@model WebApplication10.Models.ScheduleViewModel@using (Html.BeginForm()){<table class="table table-striped"><头><tr><th></th>@{foreach (Enum.GetValues(typeof(Day)) 中的第 t 天){<th>@t</th>}}</tr></thead>@{for (int i = 1; i <= 4; i++){<tr><td>周@i</td>@foreach(Enum.GetValues(typeof(Day)) 中的第 t 天){<td><input type="checkbox"/></td>}</tr>}}</tbody><div class="form-group"><div class="col-md-offset-2 col-md-10"><input type="submit" value="Create" class="btn btn-default"/>

}

我如何成功发布已选中的复选框?我的 ViewModel 有意义吗?

解决方案

我建议您将视图模型更改为

公共类DayVM{公众日日{得到;放;}public bool IsSelected { 获取;放;}}公开课 WeekVM{公共 WeekVM(){天数 = 新列表();Days.Add(new DayVM() { Day = Day.Sunday });Days.Add(new DayVM() { Day = Day.Monday });.. 等等}公共列表天{得到;放;}}公共类 ScheduleViewModel{公共 ScheduleViewModel(){周数 = 新列表();Weeks.Add(new WeekVM());.... 等等}公共 int PatientId { 获取;放;}公共列表<WeekVM>周{得到;放;}}

然后在视图中

for(int i = 0; i  m.Weeks[i].Days[j].IsSelected)</td>}</tr>}

旁注:我不认为你真的需要你自己的枚举 - 你可以只使用 DayOfWeek 枚举,例如 Days.Add(new DayVM() { Day =DayOfWeek.Sunday });.另请注意,我没有包含 Day 属性的输入,因为您可以从集合中的索引轻松确定它.事实上,如果您手动呈现表格标题行,则可能根本不需要 Day 属性.

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

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.

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

查看全文
相关文章
C#/.NET最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆