一个支持普通分页和综合分页的MVC分页Helper

2019-05-22 09:17:27刘景俊

PagerQuery.cs包含两个属性,一个是PageInfo实体类属性Pager,包含RecordCount,CurrentPageIndex,PageSize三个属性。一个是Model EntityList属性。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace System.Web.Mvc
{
 public class PagerQuery<TPager,TEntityList>
 {
 public PagerQuery(TPager pager, TEntityList entityList)
 {
  this.Pager = pager;
  this.EntityList = entityList;
 }
 public TPager Pager { get; set; }
 public TEntityList EntityList { get; set; } 
 }
}

PageInfo.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace System.Web.Mvc
{
 public class PagerInfo
 {
 public int RecordCount { get; set; }

 public int CurrentPageIndex { get; set; }

 public int PageSize { get; set; }
 }
}

使用示例:

@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<PagerQuery<PagerInfo, IList<NewsArticleInfo>>>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
 NewsList
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

 <h2>NewsList</h2>

 <table>
 <tr>
  <th></th>
  <th>
  NoteID
  </th>
  <th>
  Title
  </th>
  <th>
  Author
  </th>
  <th>
  Hit
  </th>
  <th>
  ReplyNum
  </th>
  
 </tr>

 <% foreach (var item in Model.EntityList) { %>
 
 <tr>
  <td>
  <%= Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) %> |
  <%= Html.ActionLink("Details", "NewsDetail", new { noteID=item.NoteID })%>
  </td>
  <td>
  <%= Html.Encode(item.NoteID) %>
  </td>
  <td>
  <%= Html.Encode(item.Title) %>
  </td>
  <td>
  <%= Html.Encode(item.Author)%>
  </td>
  <td>
  <%= Html.Encode(item.Hit)%>
  </td>
  <td>
  <%= Html.Encode(item.ReplyNum)%>
  </td>
  
 </tr>
 
 <% } %>

 </table>

 <p>
 <%=Html.Pager("pager",Model.Pager.CurrentPageIndex,Model.Pager.PageSize,Model.Pager.RecordCount,PageMode.Numeric) %>
 </p>

</asp:Content>

 controler:

[AcceptVerbs(HttpVerbs.Get)]
 public ActionResult NewsList(int boardID,int? page)
 {
  PagerInfo pager = new PagerInfo();
  NewsArticleInfo info = new NewsArticleInfo();
  info.NewsBoard = new NewsBoardInfo();
  info.NewsBoard.BoardID = boardID;
  pager.RecordCount = Resolve<INewsBLL>().GetArticleDataList(info, ArticleTypeEnum.Pass);
  pager.PageSize = 10;
  pager.CurrentPageIndex = (page!=null?(int)page:1);
  IList<NewsArticleInfo> result = Resolve<INewsBLL>().GetArticleDataList(pager.CurrentPageIndex, pager.PageSize, ArticleTypeEnum.Pass, info);
  PagerQuery<PagerInfo, IList<NewsArticleInfo>> query = new PagerQuery<PagerInfo, IList<NewsArticleInfo>>(pager,result);
  return View(query);
 }