簡體   English   中英

使用 Linq 在視圖中顯示字段名稱

[英]Display Field Name in a View Using Linq

我想在視圖(報告)中顯示字段的屬性名稱。 該模型:

public class Report
    {
        [Display(Name ="Total Attendance")]
        public int Attendance { get; set; }

        [Display(Name = "Total Offering")]
        [DataType(DataType.Currency)]
        public double Amount { get; set; }

        [Display(Name = "District Name")]
        public int LocationId { get; set; }


        [ForeignKey("LocationId")]
        public Location Location { get; set; }
        [Display(Name = "Weekly Service")]
        public int WeeklyServiceId { get; set; }

        [ForeignKey("WeeklyServiceId")]
        [Display(Name = "Weekly Service")]
        public WeeklyService WeeklyService { get; set; }

        public DateTime? Sdate { get; set; }
        public DateTime? Edate { get; set; }

        public string UsherName { get; set; }
        public string LocationName { get; set; }
    }

控制器:

[HttpPost]
        [ValidateAntiForgeryToken]
        public IActionResult Report(Report model)
        {
            var startDate = model.Sdate;
            var endDate = model.Edate;
            var QueryResult = (from pay in _context.PaymentRecords.Include(p=>p.Location).Include(p=>p.WeeklyService)
                               //join a in _context.Locations on pay.LocationId equals a.Id
                               //join c in _context.WeeklyServices on pay.WeeklyServiceId equals c.Id
                               where (pay.DepositDate.Date >= startDate)
                               where (pay.DepositDate.Date <= endDate)
                               group pay by new { pay.LocationId,pay.WeeklyServiceId} into g
                               orderby g.Key.LocationId
                               select new Report
                               {
                                   LocationId= g.Key.LocationId,
                                   Attendance = g.Sum(x => x.Attendance),
                                   Amount = g.Sum(x => x.Amount),
                                   WeeklyServiceId =g.Key.WeeklyServiceId

                               });

            return View("Report", QueryResult);
        }

視圖/報告

<table class="table table-striped table-bordered" id="myTable">
    <thead class="thead-dark">
        <tr>
            <th>SN</th>
            <th>@Html.DisplayNameFor(model => model.Location)</th>
            <th>@Html.DisplayNameFor(model => model.Attendance)</th>
            <th>@Html.DisplayNameFor(model => model.Amount)</th>
            <th>@Html.DisplayNameFor(model => model.WeeklyService)</th>



        </tr>
    </thead>
    <tbody>
        @if (Model.Count() > 0)
        {
         int c = 0;
            foreach (var item in Model)
            {
                c++;
                <tr>
                    <td>@c</td>
                    <td>@Html.DisplayFor(modelItem =>  item.Location.Name)</td>
                    <td>@Html.DisplayFor(modelItem=>item.Attendance)</td>
                    <td>@item.Amount.ToString("C")</td>
                    <td>@Html.DisplayFor(modelItem=>item.WeeklyService.Name)</td>


                </tr>
            }
            }

            else
            {

            }
    </tbody>
</table>

以上結果在此處輸入圖片說明 請注意,Location 是一個模型,它具有 Name 作為屬性以及 WeeklyService。 但是,如果我將表數據更改為 LocationId 和 WeeklyServiceId,它將顯示帶有 Id 的結果。 但我希望它顯示位置和 WeeklyService 的名稱而不是它們的 ID。 在此處輸入圖片說明

這是我們在評論中的意思的示例。 您沒有正確初始化 Report 對象。

[HttpPost]
    [ValidateAntiForgeryToken]
    public IActionResult Report(Report model)
    {
        var startDate = model.Sdate;
        var endDate = model.Edate;
        var QueryResult = (from pay in _context.PaymentRecords.Include(p=>p.Location).Include(p=>p.WeeklyService).ToList()
                           //join a in _context.Locations on pay.LocationId equals a.Id
                           //join c in _context.WeeklyServices on pay.WeeklyServiceId equals c.Id
                           where (pay.DepositDate.Date >= startDate)
                           where (pay.DepositDate.Date <= endDate)
                           group pay by new { pay.LocationId,pay.WeeklyServiceId} into g
                           orderby g.Key.LocationId
                           select new Report
                           {
                               LocationId= g.Key.LocationId,
                               Attendance = g.Sum(x => x.Attendance),
                               Amount = g.Sum(x => x.Amount),
                               WeeklyServiceId =g.Key.WeeklyServiceId
                               Location = g.Select(pp => pp.Location).First() // This is what you are missing
                               WeeklyService  = g.Select(pp => pp.WeeklyService ).First()// Also this

                           });

        return View("Report", QueryResult);
    }

Location 和 WeeklyService 為空。 它們永遠不會被初始化。

我很驚訝您沒有收到 Null Ref Exception。 你從來沒有提到過一個。 我這么說是因為您的視圖中的(item => item.Location.Name) 希望這可以幫助。

注意小心使用客戶端的評估和EF核心3 https://github.com/dotnet/efcore/issues/17878https://github.com/dotnet/efcore/issues/17068

也取自文檔: https : //docs.microsoft.com/en-us/ef/core/what-is-new/ef-core-3.0/break-changes#linq-queries-are-no-longer-客戶評估

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM