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

ASP.NET MVC视图中如何按当日、当周、当月、当年过滤数据?

ASP.NET MVC 按当日/当周/当月/当年过滤数据实现方案

一、视图内直接过滤(VB.NET 语法)

针对你当前使用的VB.NET Lambda表达式,以下是各时间段的具体过滤逻辑:

1. 当日记录(优化版)

如果new_submittedon是带时间的DateTime类型,建议对比日期部分避免时间干扰:

@For Each item In Model.Where(Function(w) w.new_submittedon.Date = Date.Today.Date)
    ' 渲染单条记录的逻辑
Next

2. 当周记录

根据当前系统文化的一周起始日(中文环境默认周一)计算区间,如需强制周日为起始,替换FirstDayOfWeek为System.DayOfWeek.Sunday即可:

@Dim currentCulture = System.Globalization.CultureInfo.CurrentCulture
@Dim weekStart = Date.Today.Date.AddDays(-CInt(currentCulture.DateTimeFormat.FirstDayOfWeek) - CInt(Date.Today.DayOfWeek))
@Dim weekEnd = weekStart.AddDays(6).Date

@For Each item In Model.Where(Function(w) w.new_submittedon.Date >= weekStart AndAlso w.new_submittedon.Date <= weekEnd)
    ' 渲染单条记录的逻辑
Next

3. 当月记录

直接获取当月首尾日期作为过滤区间:

@Dim monthStart = New Date(Date.Today.Year, Date.Today.Month, 1)
@Dim monthEnd = monthStart.AddMonths(1).AddDays(-1).Date

@For Each item In Model.Where(Function(w) w.new_submittedon.Date >= monthStart AndAlso w.new_submittedon.Date <= monthEnd)
    ' 渲染单条记录的逻辑
Next

4. 当年记录

获取当年首尾日期作为过滤区间:

@Dim yearStart = New Date(Date.Today.Year, 1, 1)
@Dim yearEnd = New Date(Date.Today.Year, 12, 31).Date

@For Each item In Model.Where(Function(w) w.new_submittedon.Date >= yearStart AndAlso w.new_submittedon.Date <= yearEnd)
    ' 渲染单条记录的逻辑
Next

二、更优方案:Controller层预处理数据

视图层尽量只负责渲染,建议在Controller中提前筛选好各时间段数据,封装到ViewModel中:

1. 定义ViewModel

Public Class RecordsViewModel
    Public Property TodayRecords As List(Of YourRecordModel)
    Public Property WeekRecords As List(Of YourRecordModel)
    Public Property MonthRecords As List(Of YourRecordModel)
    Public Property YearRecords As List(Of YourRecordModel)
End Class

2. Controller中筛选数据

Public Function Index() As ActionResult
    Dim allRecords = db.YourRecords.ToList() ' 从数据库获取全量数据
    Dim vm = New RecordsViewModel()
    
    ' 当日数据
    vm.TodayRecords = allRecords.Where(Function(r) r.new_submittedon.Date = Date.Today.Date).ToList()
    
    ' 当周数据
    Dim currentCulture = System.Globalization.CultureInfo.CurrentCulture
    Dim weekStart = Date.Today.Date.AddDays(-CInt(currentCulture.DateTimeFormat.FirstDayOfWeek) - CInt(Date.Today.DayOfWeek))
    Dim weekEnd = weekStart.AddDays(6).Date
    vm.WeekRecords = allRecords.Where(Function(r) r.new_submittedon.Date >= weekStart AndAlso r.new_submittedon.Date <= weekEnd).ToList()
    
    ' 当月数据
    Dim monthStart = New Date(Date.Today.Year, Date.Today.Month, 1)
    Dim monthEnd = monthStart.AddMonths(1).AddDays(-1).Date
    vm.MonthRecords = allRecords.Where(Function(r) r.new_submittedon.Date >= monthStart AndAlso r.new_submittedon.Date <= monthEnd).ToList()
    
    ' 当年数据
    Dim yearStart = New Date(Date.Today.Year, 1, 1)
    Dim yearEnd = New Date(Date.Today.Year, 12, 31).Date
    vm.YearRecords = allRecords.Where(Function(r) r.new_submittedon.Date >= yearStart AndAlso r.new_submittedon.Date <= yearEnd).ToList()
    
    Return View(vm)
End Function

3. 视图中直接渲染

' 当日标签页
<div class="tab-panel active" id="today-tab">
    @For Each item In Model.TodayRecords
        ' 渲染单条记录的逻辑
    Next
</div>

' 当周标签页
<div class="tab-panel" id="week-tab">
    @For Each item In Model.WeekRecords
        ' 渲染单条记录的逻辑
    Next
</div>

' 当月标签页
<div class="tab-panel" id="month-tab">
    @For Each item In Model.MonthRecords
        ' 渲染单条记录的逻辑
    Next
</div>

' 当年标签页
<div class="tab-panel" id="year-tab">
    @For Each item In Model.YearRecords
        ' 渲染单条记录的逻辑
    Next
</div>

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.16 02:05:19