MVC——分页
添加类PageBar.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web; namespace System.Web.Mvc
{
public static class PageBar
{
public static HtmlString ShowPageNavigate(this HtmlHelper htmlHelper, int currentPage, int pageSize, int totalCount)
{
var output = new StringBuilder(); var redirectTo = htmlHelper.ViewContext.RequestContext.HttpContext.Request.Url.AbsolutePath;
pageSize = pageSize == 0 ? 10 : pageSize;
var totalPages = Math.Max((totalCount + pageSize - 1) / pageSize, 1); //总页数
int MaxPagerCount = 10;
int visibleStartMax=1;
if (totalPages > MaxPagerCount)
{
visibleStartMax = totalPages - (MaxPagerCount - 1); }
int visibleStart = currentPage - MaxPagerCount / 2;//起始页
if (visibleStart < 1)
{
visibleStart = 1;
}
if (visibleStart > visibleStartMax)
{
visibleStart = visibleStartMax;
}
int visibleEnd = visibleStart + MaxPagerCount-1;//结束页码
//如果算出来的结束页码大于总页码的话则调整为最大页码
if (visibleEnd > totalPages)
{
visibleEnd = totalPages;
}
if (currentPage == 1)
{
output.Append("<span>首页</span>");
output.Append(" ");
output.Append("<span>上一页</span>");
output.Append(" ");
} if (currentPage > 1)
{
output.AppendFormat("<a class='pageLink' href='{0}?pageIndex=1&pageSize={1}'>首页</a> ", redirectTo, pageSize);
//处理上一页的连接
output.AppendFormat("<a class='pageLink' href='{0}?pageIndex={1}&pageSize={2}'>上一页</a> ", redirectTo, currentPage - 1, pageSize);
}
output.Append(" ");
//绘制可视的页码链接
for (int i = visibleStart; i <= visibleEnd; i++)
{
//当前页不是超链接
if (i == currentPage)
{
output.Append("<span>").Append(i).Append("</span>");
}
else //一般页处理
{
output.AppendFormat("<a class='pageLink' href='{0}?pageIndex={1}&pageSize={2}'>{3}</a> ", redirectTo, i, pageSize, i);
}
output.Append(" ");
}
if (currentPage < totalPages)
{
output.AppendFormat("<a class='pageLink' href='{0}?pageIndex={1}&pageSize={2}'>下一页</a> ", redirectTo, currentPage + 1, pageSize);
output.Append(" ");
output.AppendFormat("<a class='pageLink' href='{0}?pageIndex={1}&pageSize={2}'>末页</a> ", redirectTo, totalPages, pageSize);
output.Append(" ");
}
else
{
output.Append("<span>下一页</span>");
output.Append(" ");
output.Append("<span>末页</span>");
output.Append(" ");
}
output.AppendFormat("第{0}页 / 共{1}页", currentPage, totalPages);//这个统计加不加都行 return new HtmlString(output.ToString()); }
}
}
添加控制器AjaxController
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplicationStudy.Models;
namespace MvcApplicationStudy.Controllers
{
public class AjaxController : Controller
{
//
// GET: /Ajax/ public ActionResult Index()
{
return View();
} //分页
public ActionResult ShowUserInfoList()
{
TestEntities db = new TestEntities();
int pageIndex;
if(!int.TryParse(Request["pageIndex"],out pageIndex ))
{
pageIndex = 1;
}
int pageSize = 1;
int totalCount = db.UserInfo.Count();
int pageCount = Convert.ToInt32(Math.Ceiling((double)totalCount / pageSize));
if (pageIndex < 1)
pageIndex = 1;
if (pageIndex > pageCount)
pageIndex = pageCount;
var userInfoList = db.UserInfo.Where<UserInfo>(u => true).OrderBy<UserInfo, int>(u => u.ID).Skip<UserInfo>((pageIndex - 1) * pageSize).Take<UserInfo>(pageSize);
List<UserInfo> list = userInfoList.ToList();
ViewData.Model = list;
ViewBag.PageIndex = pageIndex;
ViewBag.PageSize = pageSize;
ViewBag.PageCount = pageCount;
ViewBag.TotalCount = totalCount; return View(); }
}
}
添加视图 ShowUserInfoList
@model IEnumerable<MvcApplicationStudy.Models.UserInfo>
@{
Layout = null;
} <!DOCTYPE html> <html>
<head>
<meta name="viewport" content="width=device-width" />
<title>ShowUserInfoList</title>
</head>
<body>
<div>
<table>
<tr><th>@Html.DisplayNameFor(model=>model.UserName)</th>
<th>@Html.DisplayNameFor(model=>model.UserPwd)</th>
<th>@Html.DisplayNameFor(model=>model.RegTime)</th></tr>
@foreach (var item in Model)
{
<tr><td>@Html.DisplayFor(modelItem=>item.UserName)</td>
<td>@Html.DisplayFor(modelItem=>item.UserPwd )</td>
<td>@Html.DisplayFor(modelItem=>item.RegTime )</td> </tr>
}
</table>
@for (int i = 1; i <= (int)@ViewBag.PageCount; i++)
{
@Html.ActionLink(i.ToString(), "ShowUserInfoList", new { pageIndex=i});
}
<br />
<br />
@Html.ShowPageNavigate((int)ViewBag.PageIndex,(int)ViewBag.PageSize,(int)ViewBag.TotalCount);
</div>
</body>
</html>
运行结果
MVC——分页的更多相关文章
- MVC分页
http://www.cnblogs.com/iamlilinfeng/p/4075292.html 目录 一.Contrl与View数据传递(多表数据) 二.分页控件介绍 三.MVC源码说明 四.源 ...
- ASP.NET MVC分页组件MvcPager 2.0版发布暨网站全新改版
MvcPager分页控件是在ASP.NET MVC Web应用程序中实现分页功能的一系列扩展方法,该分页控件的最初的实现方法借鉴了网上流行的部分源代码, 尤其是ScottGu的PagedList< ...
- ASP.NET MVC 4使用PagedList.Mvc分页
ASP.NET MVC中进行分页的方式有多种,在NuGet上有提供使用PagedList.PagedList.Mvc进行分页. 1. 通过NuGet引用PagedList.Mvc 在安装引用Paged ...
- ASP.NET MVC分页实现之改进版-增加同一个视图可设置多个分页
我之前就已经实现了ASP.NET MVC分页(查看该博文),但它有局限性,必须确保在同一个视图中只能有一处分页,若需要在同一个视图中设置多个分页,却无能为力,为此,我重新对原先的代码进行了优化,增加了 ...
- Mvc 分页栏扩展方法
using System; using System.Collections.Generic; using System.Reflection; using System.Text; using Sy ...
- 转:MVC分页
原文地址:http://www.cnblogs.com/iamlilinfeng/p/4075292.html 分页总是搞得我很烦,也是因为刚接触,貌似有好多插件,之前在用一个,可是后来发现一翻页原来 ...
- spring mvc 分页
spring mvc 分页
- 基于Bootstrap的Asp.net Mvc 分页
基于Bootstrap的Asp.net Mvc 分页的实现 最近写了一个mvc 的 分页,样式是基于 bootstrap 的 ,提供查询条件,不过可以自己写样式根据个人的喜好,以此分享一下.首先新建一 ...
- Mvc分页组件MvcSimplePager代码重构
1 Mvc分页组件MvcSimplePager代码重构 1.1 Intro 1.2 MvcSimplePager 代码优化 1.3 MvcSimplePager 使用 1.4 End Mvc分页组件M ...
- Mvc分页组件MvcSimplePager代码重构及使用
1 Mvc分页组件MvcSimplePager代码重构 1.1 Intro 1.2 MvcSimplePager 代码优化 1.3 MvcSimplePager 使用 1.4 End Mvc分页组件M ...
随机推荐
- chef and churu 分块 好题
题目大意 有一个长度为n的数组A 有n个函数,第i个函数 \[f(l[i],r[i])=\sum_{k=l[i]}^{r[i]}A_k\] 有两种操作: 1)修改A[i] 2)询问第x-y个函数值的和 ...
- 10个JavaScript难点--摘抄
1. 立即执行函数 立即执行函数,即Immediately Invoked Function Expression (IIFE),正如它的名字,就是创建函数的同时立即执行.它没有绑定任何事件,也无需等 ...
- VirtualBox 與 Vmware 差異
VirtualBox 4.3.36_Ubuntu r105129 與 VMware® Workstation 12 Player 12.5.2 build-4638234, 分別在各自的 Ubunt ...
- Day 22 生成器yield表达式及内置函数(一丢丢)
本日知识点: ################################### #一.上节课复习:在for循环式,调用对象内部的__iter__方法, # 把他们变成了可迭代对象然后for循环调 ...
- 开发使用mysql的一些必备知识点整理(一)初阶
简介 主要知识点包括:能够与mysql建立连接,创建数据库.表,分别从图形界面与脚本界面两个方面讲解 相关的知识点包括:E-R关系模型,数据库的3范式,mysql中数据字段的类型,字段约束 数据库的操 ...
- zookeeper源码 — 四、session建立
目录 session建立的主要过程 客户端发起连接 服务端创建session session建立的主要过程 用一张图来说明session建立过程中client和server的交互 主要流程 服务端启动 ...
- ML | k-means
what's xxx k-means clustering aims to partition n observations into k clusters in which each observa ...
- Symmetric Tree(DFS,二叉树的构建以及测试代码)
基础有待加强啊,由该题引发出来一些问题,现在来总结下. 首先是二叉树的结构: struct TreeNode { EleType val; TreeNode *left; TreeNode *righ ...
- OpenLDAP给我的启发
首先这篇文章没什么技术性,但亮点是:我会给广大运维同行提一点建议,这个一点仅仅是一点,而不是很多点. 年前计划深度掌握一些诸如:Jenkins.Gitlab.ELK.k8s等的软件,但学着学着总是想学 ...
- Codeforces 401D Roman and Numbers
题目大意 Description 给定一个数 N(N<1018) , 求有多少个经过 N 重组的数是 M(M≤100) 的倍数. 注意: ①重组不能有前导零; ②重组的数相同, 则只能算一个数. ...