亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

MVC 問題創建基于當前周迭代視圖的表

MVC 問題創建基于當前周迭代視圖的表

C#
侃侃無極 2022-01-09 10:07:48
截至目前,我的視圖上有一個表格,如下所示:此表的目的是顯示當前一周內每個位置(左側)發生的次數。我的數據庫中有 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因為在本周這些位置的關聯表中沒有任何其他記錄。
查看完整描述

2 回答

?
繁星點點滴滴

TA貢獻1803條經驗 獲得超3個贊

我不確定我是否做對了,但我想你需要的是這個,每次你循環你的位置時,你應該再次循環你的日期,然后計算或求和發生計數(取決于你的系統設計我沒有'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>

        }


查看完整回答
反對 回復 2022-01-09
?
回首憶惘然

TA貢獻1847條經驗 獲得超11個贊

我已經想通了。


在我看來,我將table tbody代碼編輯為:


<tbody>

    @foreach (var weighLocation in Model.WeighLocations)

    {

        <tr>

            <td>@weighLocation.Weigh_Location</td>

            @foreach (var date in Model.CurrentWeekDates)

            {

                if (Model.WeighAssociations.Any(x => x.tbl_TEUForm.EventDate == date && x.WeighLocationId == weighLocation.ID))

                {

                    <td>@Model.WeighAssociations.Single(x => x.tbl_TEUForm.EventDate == date && x.WeighLocationId == weighLocation.ID).OccurenceCount</td>

                }

                else

                {

                    <td>0</td>

                }


            }

        </tr>

    }

</tbody>


查看完整回答
反對 回復 2022-01-09
  • 2 回答
  • 0 關注
  • 132 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號