You need to enable JavaScript to run this app. ASP.NET Core中如何使用Razor实现Timesheet列的数值求和
优惠活动
大模型
产品
解决方案
定价
更多

ASP.NET Core中如何使用Razor实现Timesheet列的数值求和

工时列求和实现方案

1. 控制器侧聚合逻辑调整

不要仅传递原始数据列表到视图,直接在控制器端完成各周字段的总和计算,通过ViewBag传递给视图,同时修正原有方法的视图返回路径错误:

// 全量数据汇总页
public async Task<IActionResult> ShowSummary()
{
    var allRecords = await _context.Timesheet.ToListAsync();
    // 数据库侧直接计算各列总和,避免全量数据拉到内存计算的性能损耗
    ViewBag.TotalMon = await _context.Timesheet.SumAsync(x => x.Mon);
    ViewBag.TotalTue = await _context.Timesheet.SumAsync(x => x.Tue);
    ViewBag.TotalWed = await _context.Timesheet.SumAsync(x => x.Wed);
    ViewBag.TotalThr = await _context.Timesheet.SumAsync(x => x.Thr);
    ViewBag.TotalFri = await _context.Timesheet.SumAsync(x => x.Fri);
    ViewBag.TotalSat = await _context.Timesheet.SumAsync(x => x.Sat);
    ViewBag.TotalSun = await _context.Timesheet.SumAsync(x => x.Sun);
    // 跨列总合计
    ViewBag.TotalWeekAll = ViewBag.TotalMon + ViewBag.TotalTue + ViewBag.TotalWed + ViewBag.TotalThr + ViewBag.TotalFri + ViewBag.TotalSat + ViewBag.TotalSun;
    
    return View(allRecords);
}

// 按用户ID过滤的汇总结果
public async Task<IActionResult> ShowSummaryResults(int SearchPhrase)
{
    var userRecords = await _context.Timesheet.Where(j => j.UserID == SearchPhrase).ToListAsync();
    // 计算单用户维度的各列工时总和
    ViewBag.TotalMon = userRecords.Sum(x => x.Mon);
    ViewBag.TotalTue = userRecords.Sum(x => x.Tue);
    ViewBag.TotalWed = userRecords.Sum(x => x.Wed);
    ViewBag.TotalThr = userRecords.Sum(x => x.Thr);
    ViewBag.TotalFri = userRecords.Sum(x => x.Fri);
    ViewBag.TotalSat = userRecords.Sum(x => x.Sat);
    ViewBag.TotalSun = userRecords.Sum(x => x.Sun);
    ViewBag.TotalWeekAll = ViewBag.TotalMon + ViewBag.TotalTue + ViewBag.TotalWed + ViewBag.TotalThr + ViewBag.TotalFri + ViewBag.TotalSat + ViewBag.TotalSun;
    
    // 修正原有错误:原来返回Index视图会导致页面布局不匹配,改回汇总视图
    return View("ShowSummary", userRecords);
}

2. Razor视图(ShowSummary.cshtml)实现

在视图中同时展示单条记录的周工时合计、全表/筛选后结果的列汇总值:

@model IEnumerable<TimeSheat.Models.Timesheet>

@{
    ViewData["Title"] = "工时汇总";
}

<table class="table table-bordered">
    <thead>
        <tr>
            <th>用户ID</th>
            <th>项目</th>
            <th>周一</th>
            <th>周二</th>
            <th>周三</th>
            <th>周四</th>
            <th>周五</th>
            <th>周六</th>
            <th>周日</th>
            <th>单条记录周工时</th>
        </tr>
    </thead>
    <tbody>
        @foreach (var record in Model)
        {
            <tr>
                <td>@record.UserID</td>
                <td>@record.Project</td>
                <td>@record.Mon</td>
                <td>@record.Tue</td>
                <td>@record.Wed</td>
                <td>@record.Thr</td>
                <td>@record.Fri</td>
                <td>@record.Sat</td>
                <td>@record.Sun</td>
                // 单条记录直接计算七个字段的和
                <td>@(record.Mon + record.Tue + record.Wed + record.Thr + record.Fri + record.Sat + record.Sun)</td>
            </tr>
        }
    </tbody>
    <!-- 表尾固定展示列汇总值 -->
    <tfoot>
        <tr class="table-active">
            <td colspan="2">所有记录总合计</td>
            <td>@ViewBag.TotalMon</td>
            <td>@ViewBag.TotalTue</td>
            <td>@ViewBag.TotalWed</td>
            <td>@ViewBag.TotalThr</td>
            <td>@ViewBag.TotalFri</td>
            <td>@ViewBag.TotalSat</td>
            <td>@ViewBag.TotalSun</td>
            <td>@ViewBag.TotalWeekAll</td>
        </tr>
    </tfoot>
</table>

注意事项

  • 现有代码中周四对应的字段名拼写为Thr,属于不规范命名(行业通用缩写为Thu),后续维护建议修正,修正后需要同步更新EF迁移脚本,避免后续开发产生歧义
  • 大数量级数据的全表聚合,优先用EF Core内置的SumAsync在数据库端完成计算,不要把全表数据拉到内存后在视图里做求和,会产生不必要的性能损耗
  • 原有ShowSummaryResults方法返回Index视图的逻辑会导致页面元素、导航不匹配,必须修正为返回对应汇总视图才能正常展示求和结果

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.28 01:06:04