我正在尝试创建一个用户可以使用我的Index.cshtml
过滤的报告。报告工作正常,我在页脚有一个总计,工作得很好。
我的问题是,我需要的第二个总数不是基于一个特定的领域。它基于"if"过滤器。我尝试的每个变量都会返回各种错误。这一切对我来说还很新鲜,我肯定我错过了一些明显的东西。希望有人能给我指出来。
下面是表中相关部分的代码:
<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>
这是我的.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;
}
}
}
非常感谢您提供的任何见解!
您可以使用Where
方法筛选项目,以便仅将DISPFinePdInFull
设置为true
的项目包含在总数中:
<td>
@Model.Results.Where(x => x.DISPFinePdInFull).Sum(x => x.DISPFineAmount)
</td>
从OP:得到这个工作与一个轻微的编辑:
@Model.Results。在(x =比;x.p disfinepdinfull== "Yes">(.总和(x =比;x.DISPFineAmount)