Mvc分页系统使用月份而不是页码

时间:2013-08-06 08:48:31

标签: asp.net-mvc

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

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> 
  }

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

这是返回我的viewmodel

的服务方法
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);
    }

1 个答案:

答案 0 :(得分: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);

如果selectedTimeDateTime,则selectedTime.Month将是默认月份(如果未提供所需的值,可能为now.Month)或所选月份(如果是已提供所需的值)。期望的月份值很可能是路线值。同样适用于selectedTime.Year

您还可以通过链接到路径值为prevTime.MonthprevTime.Year(其中为var prevTime = selectedTime.AddMonths(-1))或{{的nextTime.Month的相同网页,提供指向下一个月和前几个月的链接1}}和nextTime.Year(其中var nextTime = selectedTime.AddMonths(1))。