简体   繁体   English

使用 Linq 在视图中显示字段名称

[英]Display Field Name in a View Using Linq

I want to display the property name of a field in a view(report).我想在视图(报告)中显示字段的属性名称。 The Model:该模型:

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; }
    }

Controller:控制器:

[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);
        }

The View/Report视图/报告

<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>

Result of the Above以上结果在此处输入图片说明 Note that Location is a model which has Name as a property as well as WeeklyService.请注意,Location 是一个模型,它具有 Name 作为属性以及 WeeklyService。 But if i change the table data to LocationId and WeeklyServiceId, it will display the results with the Id.但是,如果我将表数据更改为 LocationId 和 WeeklyServiceId,它将显示带有 Id 的结果。 But I want it to display the Name of the Location and WeeklyService instead of their Ids.但我希望它显示位置和 WeeklyService 的名称而不是它们的 ID。 在此处输入图片说明

Here is an exmaple of what we mean in the comments.这是我们在评论中的意思的示例。 You do not Initialize correctly the Report object.您没有正确初始化 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);
    }

The Location and WeeklyService is null. Location 和 WeeklyService 为空。 They are never initialized.它们永远不会被初始化。

I am surprised you do not get a Null Ref Exception.我很惊讶您没有收到 Null Ref Exception。 You never mentioned one.你从来没有提到过一个。 I am saying this because of the (item => item.Location.Name) in your View.我这么说是因为您的视图中的(item => item.Location.Name) Hope this helps.希望这可以帮助。

Note careful with client side evaluation and EF core 3 https://github.com/dotnet/efcore/issues/17878 , https://github.com/dotnet/efcore/issues/17068注意小心使用客户端的评估和EF核心3 https://github.com/dotnet/efcore/issues/17878https://github.com/dotnet/efcore/issues/17068

Also taken from the docs: https://docs.microsoft.com/en-us/ef/core/what-is-new/ef-core-3.0/breaking-changes#linq-queries-are-no-longer-evaluated-on-the-client也取自文档: 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