You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Razor Page中实现按年度统计申请类型数量并生成表格

问题解决方案

1. 修正后的后台PageModel代码

public class ReferralStatsModel : PageModel
{
    private readonly YourDbContext _context;

    public ReferralStatsModel(YourDbContext context)
    {
        _context = context;
    }

    [BindProperty(SupportsGet = true)]
    public int Year1 { get; set; } = DateTime.Now.Year;

    [BindProperty(SupportsGet = true)]
    public int Year2 { get; set; } = DateTime.Now.Year - 1;

    [BindProperty(SupportsGet = true)]
    public int Year3 { get; set; } = DateTime.Now.Year - 2;

    public List<ApplicationTypeStats> Stats { get; set; }
    public YearTotals Totals { get; set; }

    public class ApplicationTypeStats
    {
        public string ApplicationType { get; set; }
        public int Year1Count { get; set; }
        public int Year2Count { get; set; }
        public int Year3Count { get; set; }
    }

    public class YearTotals
    {
        public int Year1Total { get; set; }
        public int Year2Total { get; set; }
        public int Year3Total { get; set; }
    }

    public async Task OnGetAsync()
    {
        // 获取所有唯一的申请类型
        var applicationTypes = await _context.Referrals
            .Select(r => r.ApplicationType)
            .Distinct()
            .ToListAsync();

        Stats = new List<ApplicationTypeStats>();

        foreach (var type in applicationTypes)
        {
            var stats = new ApplicationTypeStats
            {
                ApplicationType = type,
                Year1Count = await _context.Referrals
                    .CountAsync(r => r.ApplicationType == type && r.BoardMtgYear == Year1),
                Year2Count = await _context.Referrals
                    .CountAsync(r => r.ApplicationType == type && r.BoardMtgYear == Year2),
                Year3Count = await _context.Referrals
                    .CountAsync(r => r.ApplicationType == type && r.BoardMtgYear == Year3)
            };
            Stats.Add(stats);
        }

        // 计算年度总计
        Totals = new YearTotals
        {
            Year1Total = Stats.Sum(s => s.Year1Count),
            Year2Total = Stats.Sum(s => s.Year2Count),
            Year3Total = Stats.Sum(s => s.Year3Count)
        };
    }
}

2. 修正后的Razor视图代码(右对齐表格)

@page
@model ReferralStatsModel

<div class="container">
    <h1>Referral Application Stats</h1>

    <!-- 年度输入表单 -->
    <form method="get">
        <div class="form-group row">
            <label class="col-sm-2 col-form-label">年度1:</label>
            <div class="col-sm-2">
                <input asp-for="Year1" class="form-control" />
            </div>
            <label class="col-sm-2 col-form-label">年度2:</label>
            <div class="col-sm-2">
                <input asp-for="Year2" class="form-control" />
            </div>
            <label class="col-sm-2 col-form-label">年度3:</label>
            <div class="col-sm-2">
                <input asp-for="Year3" class="form-control" />
            </div>
        </div>
        <button type="submit" class="btn btn-primary">更新统计</button>
    </form>

    <hr />

    <!-- 右对齐统计表格 -->
    <table class="table table-bordered">
        <thead>
            <tr>
                <th>申请类型</th>
                <th style="text-align:right">@Model.Year1</th>
                <th style="text-align:right">@Model.Year2</th>
                <th style="text-align:right">@Model.Year3</th>
            </tr>
        </thead>
        <tbody>
            @foreach (var stat in Model.Stats)
            {
                <tr>
                    <td>@stat.ApplicationType</td>
                    <td style="text-align:right">@stat.Year1Count</td>
                    <td style="text-align:right">@stat.Year2Count</td>
                    <td style="text-align:right">@stat.Year3Count</td>
                </tr>
            }
            <tr style="font-weight:bold">
                <td>总计</td>
                <td style="text-align:right">@Model.Totals.Year1Total</td>
                <td style="text-align:right">@Model.Totals.Year2Total</td>
                <td style="text-align:right">@Model.Totals.Year3Total</td>
            </tr>
        </tbody>
    </table>
</div>

关键修正点说明

  • 给年度参数添加SupportsGet = true,支持通过GET请求传递自定义年度值
  • 使用异步查询CountAsync优化性能,避免同步操作阻塞线程
  • 新增YearTotals类统一处理年度总计,逻辑更清晰
  • 表格列通过style="text-align:right"实现右对齐格式要求
  • 确保所有异步方法都添加await关键字,修正语法错误

内容的提问来源于stack exchange,提问作者MelB

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 09:45:43