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

MVC统计页改造:将日期设为列头展示每日早晚样本数据

ASP.NET MVC 横向日期列统计页面改造方案

1. 定义视图模型(ViewModel)

先创建适配视图展示的模型,把部门与多日期的统计数据关联起来:

// 单日期的早晚样本数据容器
public class DailySampleData
{
    public int SamplesMorning { get; set; }
    public int SamplesEvening { get; set; }
}

// 部门的多日期统计模型
public class DepartmentStatsViewModel
{
    public int DeptId { get; set; }
    public string DeptName { get; set; } // 从关联实体获取部门名称,根据实际关联调整
    public Dictionary<DateTime, DailySampleData> DateSampleMap { get; set; } 
        = new Dictionary<DateTime, DailySampleData>();
}

2. 改造控制器方法(SamplesSttat)

在控制器中把原查询结果分组转换为视图模型,同时提取所有唯一日期用于表头渲染:

public ActionResult SamplesSttat()
{
    var hospitalId = Session["HospitalId"]?.ToString();
    if (string.IsNullOrEmpty(hospitalId))
    {
        return RedirectToAction("Login");
    }

    // 查询当前医院的统计数据
    var dailyStats = db.STATISTICS_DAILY
        .Where(s => s.HospitalId == hospitalId) // 假设模型关联了HospitalId,按实际情况调整
        .OrderByDescending(s => s.DATE)
        .ToList();

    // 提取去重并排序的日期列表
    var uniqueDates = dailyStats.Select(s => s.DATE.Date)
                                .Distinct()
                                .OrderByDescending(d => d)
                                .ToList();

    // 按部门分组,构建视图模型集合
    var departmentStats = dailyStats
        .GroupBy(s => s.DEPT_ID)
        .Select(g => new DepartmentStatsViewModel
        {
            DeptId = g.Key,
            DeptName = g.First().DEPT.DEPT_NAME, // 从关联实体取部门名称,按实际调整
            DateSampleMap = g.ToDictionary(
                s => s.DATE.Date,
                s => new DailySampleData
                {
                    SamplesMorning = s.SAMPLES_MORNING ?? 0,
                    SamplesEvening = s.SAMPLES_EVENING ?? 0
                }
            )
        })
        .ToList();

    // 将日期列表传入视图
    ViewBag.UniqueDates = uniqueDates;
    return View(departmentStats);
}

3. 改造视图(SamplesSttat.cshtml)

用Razor语法渲染横向日期列的表格,每个日期下分设上午、下午两列:

@{
    ViewBag.Title = "部门样本统计";
    var uniqueDates = ViewBag.UniqueDates as List<DateTime>;
}

<h2>部门样本统计</h2>

<table class="table table-bordered">
    <thead>
        <tr>
            <th>部门名称</th>
            @foreach (var date in uniqueDates)
            {
                <th colspan="2">@date.ToString("yyyy-MM-dd")</th>
            }
        </tr>
        <tr>
            <th></th>
            @foreach (var date in uniqueDates)
            {
                <th>上午样本</th>
                <th>下午样本</th>
            }
        </tr>
    </thead>
    <tbody>
        @foreach (var deptStats in Model as List<DepartmentStatsViewModel>)
        {
            <tr>
                <td>@deptStats.DeptName</td>
                @foreach (var date in uniqueDates)
                {
                    if (deptStats.DateSampleMap.ContainsKey(date))
                    {
                        var dailyData = deptStats.DateSampleMap[date];
                        <td>@dailyData.SamplesMorning</td>
                        <td>@dailyData.SamplesEvening</td>
                    }
                    else
                    {
                        // 无数据时默认显示0,可按需改为空字符串
                        <td>0</td>
                        <td>0</td>
                    }
                }
            </tr>
        }
    </tbody>
</table>

关键说明

  • 数据聚合:通过GroupBy把同一部门的多日期数据归类,用字典映射日期与对应样本数,方便视图快速匹配数据。
  • 表头结构:先渲染日期合并列,再渲染上午/下午子列,保证表格层级清晰。
  • 空值处理:当部门在某日期无统计数据时,默认填充0,可根据业务需求调整显示内容。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.21 17:40:30