分类目录

链接

2018 年 3 月
 1234
567891011
12131415161718
19202122232425
262728293031  

近期文章

热门标签

新人福利,免费薅羊毛

现在位置:    首页 > .NET > 正文
ASP.NET MVC分页 demo
.NET 暂无评论 阅读(505)
  1. //servcie
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Web;
  6. using StaticMvc.Models;
  7.  
  8. using Webdiyer.WebControls.Mvc;
  9. namespace StaticMvc
  10. {
  11.     public class ArticleService
  12.     {
  13.         public PagedList<Article> GetPagedList(int page, int count)
  14.         {
  15.             PagedList<Article> result = GetList().ToPagedList(page, count);
  16.             result.TotalItemCount = GetList().Count();
  17.             result.CurrentPageIndex = page;
  18.  
  19.             return result;
  20.  
  21.         }
  22.  
  23.         public IQueryable<Article> GetList()
  24.         {
  25.             List<Article> list = new List<Article>();
  26.             for (int i = 1; i < 100; i++)
  27.             {
  28.                 list.Add(new Article() { Id = i, Title = "测试标题" + i, UpdateTime = DateTime.Now });
  29.             }
  30.  
  31.             return list.AsQueryable<Article>();
  32.         }
  33.     }
  34. }

 

  1. //controller
  2.   //[StaticFileFilter]
  3.         public ActionResult Index(int page = 1)
  4.         {
  5.             int count = 5;
  6.             PagedList<Article> list = articleService.GetPagedList(page, count);
  7.             ViewBag.ArticleList = list;
  8.  
  9.             return View();
  10.         }

 

  1. //html
  2. @using Webdiyer.WebControls.Mvc;
  3. @{
  4.     ViewBag.Title = "Index";
  5. }
  6.  
  7. <h2>Index</h2>
  8. <p>this is index content</p>
  9. <h1>ViewBag.ArticleList</h1>
  10. <ul>
  11.  
  12.     @foreach (StaticMvc.Models.Article item in ViewBag.ArticleList)
  13.     {
  14.         <li>@(item.Title + "(" + item.UpdateTime + ")")</li>
  15.     }
  16. </ul><br />
  17. <style>
  18.     .pager {
  19.         margin: 10px;
  20.     }
  21.  
  22.         .pager select, .pager a { 
  23.             border: 1px solid #eee;
  24.             color: #555;
  25.             text-decoration: none;
  26.             padding:5px;
  27.             font-size:14px;
  28.         }
  29.  
  30.             .pager a:hover, .pager a.current {
  31.                  border: 1px solid #2aa0f5;
  32.                 background: #2aa0f5;
  33.                 color:#fff;
  34.             }
  35. </style>
  36. @Html.Pager(ViewBag.ArticleList as PagedList<StaticMvc.Models.Article>, new PagerOptions
  37.  {
  38.      ShowDisabledPagerItems = false,
  39.      PageIndexParameterName = "page",
  40.      ShowPageIndexBox = true,
  41.      FirstPageText = "首页",
  42.      PrevPageText = "上一页",
  43.      NextPageText = "下一页",
  44.      LastPageText = "末页",
  45.      ShowGoButton = false,
  46.      CssClass = "pager",
  47.      NumericPagerItemCount = 5,
  48.      CurrentPagerItemWrapperFormatString = "<a class='current'>{0}</a>",
  49.  
  50.      PageIndexBoxType = PageIndexBoxType.DropDownList
  51.  })
  52. <br />
  53.  
  54. @Html.Partial("_SideBar");

 

附件:

MvcPager

============ 欢迎各位老板打赏~ ===========

【上篇】
【下篇】

本文版权归Bruce's Blog所有,转载引用请完整注明以下信息:
本文作者:Bruce
本文地址:ASP.NET MVC分页 demo | Bruce's Blog

发表评论

留言无头像?