首页 > 解决方案 > MVC 问题创建基于当前周迭代视图的表

问题描述

截至目前,我的视图上有一个表格,如下所示:

在此处输入图像描述

此表的目的是显示当前一周内每个位置(左侧)发生的次数。

我的数据库中有 3 个用于创建此表的表。

表一

public partial class code_WeighLocation
{
    public code_WeighLocation()
    {
        tbl_WeighAssc = new HashSet<tbl_WeighAssc>();
    }

    public int ID { get; set; }

    [Required]
    [StringLength(50)]
    public string Weigh_Location { get; set; }

    public bool Active { get; set; }

    public virtual ICollection<tbl_WeighAssc> tbl_WeighAssc { get; set; }
}

表二——关联表

public partial class tbl_WeighAssc
{
    public int Id { get; set; }

    public int WeighLocationId { get; set; }

    public int TEUId { get; set; }

    public int OccurenceCount { get; set; }

    public virtual code_WeighLocation code_WeighLocation { get; set; }

    public virtual tbl_TEUForm tbl_TEUForm { get; set; }
}

表三

public partial class tbl_TEUForm
{
    public tbl_TEUForm()
    {
        tbl_TEUArrestAssc = new HashSet<tbl_TEUArrestAssc>();
        tbl_WeighAssc = new HashSet<tbl_WeighAssc>();
        tblTEUInspectionAsscs = new HashSet<tblTEUInspectionAssc>();
    }

    public int Id { get; set; }

    public string PersonnelIBM { get; set; }

    [Column(TypeName = "date")]
    public DateTime EventDate { get; set; }

    public bool Active { get; set; }

    public virtual ICollection<tbl_TEUArrestAssc> tbl_TEUArrestAssc { get; set; }

    public virtual tblPersonnel tblPersonnel { get; set; }

    public virtual ICollection<tbl_WeighAssc> tbl_WeighAssc { get; set; }

    public virtual ICollection<tblTEUInspectionAssc> tblTEUInspectionAsscs { get; set; }
}

现在,我的观点正在接受一个视图模型:

视图模型

public class PersonnelDetailsVm
{
    private static ConnectionStringName db = new ConnectionStringName();
    public PersonnelDetailsVm()
    {
        CurrentWeekDates = new List<DateTime>();
    }
    public string IBM { get; set; }

    [Display(Name = "Name")]
    public string UserName { get; set; }

    [Display(Name = "TEU OT Rate")]
    public string Teu_OT_Rate { get; set; }

    [Display(Name = "MCSAP OT Rate")]
    public string Mcsap_OT_Rate { get; set; }

    [StringLength(10)]
    public string Division { get; set; }

    public bool Active { get; set; }

    public List<DateTime> CurrentWeekDates { get; set; }

    public List<tbl_WeighAssc> WeighAssociations { get; set; }
    public List<code_WeighLocation> WeighLocations => db.code_WeighLocation.ToList();
}

在我创建上面显示的表的视图中,我的代码如下所示:

<table class="table table-bordered table-hover mt-3">
    <thead>
        <tr>
            <th></th>
            @foreach (var date in Model.CurrentWeekDates)
            {
                <th>@date.ToString("ddd") <br /> @date.ToShortDateString()</th>
            }
        </tr>
    </thead>
    <tbody>
        @foreach (var weighLocation in Model.WeighLocations)
        {
            <tr>
                <td>@weighLocation.Weigh_Location</td>
            </tr>
        }
    </tbody>
</table>

现在,在我的数据库中,在关联表中我只有 2 条记录,并且两条记录都是在Friday, 9/7/2018输入的。一个记录是出现次数为 2 的 WIMS/SR-1。另一个记录是出现次数为 2 的 FIXED/Blackbird。所以,我的目标是在2018 年9 月 7 日星期五的相应行中显示这些计数/cells 和每个其他单元格都用 a 填充,0因为在本周这些位置的关联表中没有任何其他记录。

这是我的控制器代码,用于显示我如何填充星期几并根据这些日期获取正确的记录。

var startOfWeek = DateTime.Today.AddDays((int) CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek -
                                         (int) DateTime.Today.DayOfWeek);
person.CurrentWeekDates = Enumerable.Range(0, 7).Select(i => startOfWeek.AddDays(i)).ToList();
var teuFormIds = db.tbl_TEUForm
    .Where(x => person.CurrentWeekDates.Contains(x.EventDate) && x.PersonnelIBM == person.IBM).Select(t => t.Id).ToList();

person.WeighAssociations = db.tbl_WeighAssc.Where(x => teuFormIds.Contains(x.TEUId)).ToList();

任何帮助表示赞赏

标签: c#asp.net-mvcrazor

解决方案


我不确定我是否做对了,但我想你需要的是这个,每次你循环你的位置时,你应该再次循环你的日期,然后计算或求和发生计数(取决于你的系统设计我没有't get it)并显示它。您无需担心单元格顺序,因为您总是在同一天循环,因此它们将对齐第一个 col 始终是第一天,依此类推。还请仔细检查我使用的条件,我不确定它是否正确,但无论如何这是方法。

<!-- Loop through locations -->
     @foreach (var weighLocation in Model.WeighLocations)
        {
            <tr>
                <td>@weighLocation.Weigh_Location</td>
<!-- loop through current days week days for each location--> 
                     @foreach(var currentDay in Model.CurrentWeekDates)
                      {
<!-- Here you count the rows or maybe sum the OccurenceCount  I am not sure how you design it, I used count but you can use sum OccurenceCount  -->
                      <td>
<!-- Compare the location id and the teuForm date -->
                       @Model.WeighAssociations.Where(e => e.WeighLocationId == weighLocation.Id && e.tbl_TEUForm.EventDate == currentDay).Count()
                       </td>
                      }
            </tr>
        }

推荐阅读