MVC 问题创建基于当前周迭代视图的表

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

http://img2.mukewang.com/61da438b00019fb711420460.jpg

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


我的数据库中有 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>();

    }


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



侃侃无极
浏览 110回答 2
2回答

繁星点点滴滴

我不确定我是否做对了,但我想你需要的是这个,每次你循环你的位置时,你应该再次循环你的日期,然后计算或求和发生计数(取决于你的系统设计我没有't get it)并显示它。您无需担心单元格顺序,因为您总是在同一天循环,因此它们将对齐第一个 col 始终是第一天,依此类推。还请仔细检查我使用的条件,我不确定它是否正确,但无论如何这是方法。<!-- Loop through locations -->&nbsp; &nbsp; &nbsp;@foreach (var weighLocation in Model.WeighLocations)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>@weighLocation.Weigh_Location</td><!-- loop through current days week days for each location-->&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;@foreach(var currentDay in Model.CurrentWeekDates)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {<!-- Here you count the rows or maybe sum the OccurenceCount&nbsp; I am not sure how you design it, I used count but you can use sum OccurenceCount&nbsp; -->&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td><!-- Compare the location id and the teuForm date -->&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;@Model.WeighAssociations.Where(e => e.WeighLocationId == weighLocation.Id && e.tbl_TEUForm.EventDate == currentDay).Count()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; }

回首忆惘然

我已经想通了。在我看来,我将table tbody代码编辑为:<tbody>&nbsp; &nbsp; @foreach (var weighLocation in Model.WeighLocations)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>@weighLocation.Weigh_Location</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @foreach (var date in Model.CurrentWeekDates)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (Model.WeighAssociations.Any(x => x.tbl_TEUForm.EventDate == date && x.WeighLocationId == weighLocation.ID))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>@Model.WeighAssociations.Single(x => x.tbl_TEUForm.EventDate == date && x.WeighLocationId == weighLocation.ID).OccurenceCount</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>0</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; }</tbody>
打开App,查看更多内容
随时随地看视频慕课网APP