如何在Razor Index页面表格中计算筛选列的总计值
问题解决:带条件筛选的报表总计计算
我正在用Index.cshtml创建可用户筛选的报表,当前报表功能正常,表格页脚的总计值能正常显示。但第二个总计值需要只计算DISPFinePdInFull为"Yes"的DISPFineAmount总和,尝试的写法都报错,求指导。
现有代码
Index.cshtml相关代码
<tbody> @foreach (var obj in Model.Results) { <tr> <td width="5%" align="left">@obj.CRTCourt</td> <td width="5%" align="left">@obj.PILastName, @obj.PIFirstName @obj.PIMiddleInitial</td> <td width="10%">@obj.ArrestDate</td> <td width="10%">@obj.DISPConvictionDate</td> <td width="15%" align="left">@obj.ArrestOfficer</td> <td width="10%">@obj.DISPFineAmount</td> <td width="10%">@obj.DISPDueDate</td> <td width="5%" align="left">@obj.DISPFinePdInFull</td> <td width="5%" id="FilteredFine"> @if (@obj.DISPFinePdInFull == "Yes") { @obj.DISPFineAmount } else { <text>""</text> } </td> </tr> } </tbody> <tfoot> <tr> <td></td> <td></td> <td></td> <td></td> <td> Total Fines Assessed </td> <td> @Model.Results.Sum(i=>i.DISPFineAmount) </td> <td></td> <td> Total Pd </td> <td> <!-- (This part is the area I am struggling with. Think I need to add something to my .cs to handle this, but not sure what?) @if (Model.Results.DISPFinePdInFull == "Yes") { @Model.Results.Sum(i => i.DISPFineAmount) } else { <text>""</text> } --> </td> </tr> </tfoot> </table>
Index.cs相关代码
using DWITracker.Data; using DWITracker.Model; using Microsoft.AspNetCore.Mvc.RazorPages; using System.Data; namespace DWITracker.Pages.Reports.CourtFines; public class IndexModel : PageModel { private readonly ApplicationDbContext _db; public IndexModel(ApplicationDbContext db) { _db = db; } public IEnumerable<Incident> Results { get; set; } public string CourtSort { get; set; } public void OnGet() { Results = _db.Incident.ToList(); } public void OnPost(DateTime startdate, DateTime enddate, string sortOrder) { Results = (from x in _db.Incident where (x.DISPConvictionDate >= startdate) && (x.DISPConvictionDate <= enddate) select x).ToList(); CourtSort = sortOrder == "Court_Asc_Sort" ? "Court_Desc_Sort" : "Court_Asc_Sort"; switch (sortOrder) { case "Court_Asc_Sort": Results = Results.OrderBy(s => s.CRTCourt).ThenBy(s => s.PILastName); break; case "Court_Desc_Sort": Results = Results.OrderByDescending(s => s.CRTCourt).ThenByDescending(s => s.PILastName); break; default: Results = Results.OrderBy(s => s.CRTCourt).ThenBy(s => s.PILastName); break; } } }
解决方法
方法1:直接在视图中实现条件求和
你之前的错误是直接访问集合Model.Results的DISPFinePdInFull属性,这是不对的——集合本身没有这个属性,需要先筛选出符合条件的元素再求和。把tfoot里的注释代码替换成:
<td> @Model.Results.Where(i => i.DISPFinePdInFull == "Yes").Sum(i => i.DISPFineAmount) </td>
如果没有符合条件的记录,Sum会返回0,若想显示空字符串,可加判断:
<td> @{ var totalPaid = Model.Results.Where(i => i.DISPFinePdInFull == "Yes").Sum(i => i.DISPFineAmount); @(totalPaid > 0 ? totalPaid.ToString() : "") } </td>
方法2:在PageModel中计算(推荐)
把计算逻辑移到后端,视图只负责展示,代码更清晰且便于维护。修改IndexModel:
using DWITracker.Data; using DWITracker.Model; using Microsoft.AspNetCore.Mvc.RazorPages; using System.Data; namespace DWITracker.Pages.Reports.CourtFines; public class IndexModel : PageModel { private readonly ApplicationDbContext _db; public IndexModel(ApplicationDbContext db) { _db = db; } public IEnumerable<Incident> Results { get; set; } public decimal TotalFinesAssessed { get; set; } public decimal TotalPaidFines { get; set; } public string CourtSort { get; set; } public void OnGet() { Results = _db.Incident.ToList(); CalculateTotals(); } public void OnPost(DateTime startdate, DateTime enddate, string sortOrder) { Results = (from x in _db.Incident where (x.DISPConvictionDate >= startdate) && (x.DISPConvictionDate <= enddate) select x).ToList(); CourtSort = sortOrder == "Court_Asc_Sort" ? "Court_Desc_Sort" : "Court_Asc_Sort"; switch (sortOrder) { case "Court_Asc_Sort": Results = Results.OrderBy(s => s.CRTCourt).ThenBy(s => s.PILastName); break; case "Court_Desc_Sort": Results = Results.OrderByDescending(s => s.CRTCourt).ThenByDescending(s => s.PILastName); break; default: Results = Results.OrderBy(s => s.CRTCourt).ThenBy(s => s.PILastName); break; } CalculateTotals(); } private void CalculateTotals() { TotalFinesAssessed = Results.Sum(i => i.DISPFineAmount); TotalPaidFines = Results.Where(i => i.DISPFinePdInFull == "Yes").Sum(i => i.DISPFineAmount); } }
然后在视图的tfoot里直接调用属性:
<tfoot> <tr> <td></td> <td></td> <td></td> <td></td> <td> Total Fines Assessed </td> <td> @Model.TotalFinesAssessed </td> <td></td> <td> Total Pd </td> <td> @Model.TotalPaidFines </td> </tr> </tfoot>
内容的提问来源于stack exchange,提问作者MelB
相关产品推荐
相关产品推荐

