I am trying to create a report that a user can filter using my Index.cshtml
. The report is working correctly and I have one total in the footer that works just fine.
My issue is that the second total I need isn't based off of a specific field. It is based on an 'if' filter. Every variation I try returns a variety of errors. This is all still pretty new to me and I'm sure I'm missing something obvious. Hoping someone can point it out to me.
Here is my code for the pertinent part of the table:
<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>
And here is my .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;
}
}
}
Thanks very much for any insight you can provide!
You can use the Where
method to filter the items so that only those that have DISPFinePdInFull
set to true
are included in the total:
<td>
@Model.Results.Where(x => x.DISPFinePdInFull).Sum(x => x.DISPFineAmount)
</td>
From OP: Got this to work with a slight edit:
@Model.Results.Where(x => x.DISPFinePdInFull == "Yes").Sum(x => x.DISPFineAmount)