0

我需要为通知列表创建一个分页系统,从本质上讲,它应该按月拆分通知列表。这是我的通知视图模型:

public class IndexViewModel
{
    public string Template { get; set; }
    public DateTime Created { get; set; }
}

这是相应的视图:

 @model IEnumerable<IndexViewModel>
 @{
    ViewBag.Title = "List";
    Layout = "~/Views/Shared/_LayoutAdmin.cshtml";

    var now = DateTime.Now;
  }

<link href="@Url.Content("~/Resources/styles/Notifications/NotificationsPage.css")" rel="stylesheet" />

  @foreach (IndexViewModel item in Model)
  { 
    <div>
       @Html.Raw(item.Template)
    </div> 
  }

我需要一个非常简单的解决方案。任何想法都非常感谢。

这是返回我的视图模型的服务方法

public List<IndexViewModel> GetIndexViewModel(int? month, int? year )//(DateTime? entryDate)
    {
        //List<string> templates = GetRenderTemplates(true);

        //Tuple<template, Created, ImportanceId>
        List<Tuple<string, DateTime, int>> templates = GetTemplatesWithDateAndImportance(true);
        templates = templates.OrderBy(f => f.Item3).ThenByDescending(f => f.Item2).Select(f => f).ToList();

        if (month != null && year != null)
            templates = templates.Where(f => f.Item2.Month == month && f.Item2.Year == year).ToList();

        List<IndexViewModel> toRet = new List<IndexViewModel>();

        foreach (Tuple<string, DateTime, int> template in templates)
        {
            toRet.Add(new IndexViewModel() { 
                Template = template.Item1,  
                Created = template.Item2
            });
        }

        return toRet;
    }

这是我的行动:

[ActionName("List")]
    public ActionResult UsersList(int? month, int? year)
    {
        List<IndexViewModel> responseModel = Service.GetIndexViewModel(month, year);                            

        return View(responseModel);
    }
4

1 回答 1

0

你可以这样做:

var filteredNotifications = Model.Where(x => x.Created.Month == selectedTime.Month && x.Created.Year == selectedTime.Year);

或者(如果您确定它们已排序):

var filteredNotifications = Model.SkipWhile(x => x.Created.Month != selectedTime.Month || x.Created.Year != selectedTime.Year).TakeWhile(x => x.Created.Month == selectedTime.Month && x.Created.Year == selectedTime.Year);

selectedTime如果是DateTime,将selectedTime.Month是默认月份(可能是now.Month,如果未提供所需值)或选定月份(如果已提供所需值)。本月的期望值很可能是路线值。也一样selectedTime.Year

prevTime.Month您还可以通过链接到具有and prevTime.Year(where var prevTime = selectedTime.AddMonths(-1)) 或nextTime.Monthand nextTime.Year(where )路由值的同一页面来获得指向下个月和前几个月的链接var nextTime = selectedTime.AddMonths(1)

于 2013-08-06T09:12:59.987 回答