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 ...
随机推荐
- jQuery控件之分页控件-- kkpager v1.3使用简介
js分页展示控件,传入简单参数就能使用的分页效果控件 在线测试链接: http://pgkk.github.io/kkpager/example/pager_test.html http://pgkk ...
- Perl语言入门--4--列表
1.列表也是数组的形式:(1,'a',2,3,4) 元素可以是任意类型,变量,表达式 2.空列表:() 单元素列表:(2) .与值2不同 qw(1 $a str) #qw是用空格作为分隔符,元素 ...
- 关于Red5整合springMVC提示scope not found 的错误
https://www.cnblogs.com/qgc88:
- jar word 模板操作比较好用的工具
个人觉得比较好用的java word 模板 http://deepoove.com/poi-tl/
- SnakeYaml使用
新的项目中需要将yaml文件解析为对象,调研了决定使用snakeYaml,下面看一看怎么使用. 一.引入依赖 因为项目是使用maven构建的,所以我们在pom文件中引入snakeYaml的依赖,如下: ...
- array_map常用技巧
array_map() 函数将用户自定义函数作用到数组中的每个值上,并返回用户自定义函数作用后的带有新值的数组. 简单来说 “array_map” 会对数组中的每一项进行处理,并返回处理后的数据. 定 ...
- Go语言_RPC_Go语言的RPC
一 标准库的RPC RPC(Remote Procedure Call,远程过程调用)是一种通过网络从远程计算机程序上请求服务,而不需要了解底层网络细节的应用程序通信协议.简单的说就是要像调用本地函数 ...
- 全方位绕过软WAF攻略
0×00 前言 现在软waf较为多,就在今年夏天苦逼挖洞的日子里经常遇到360主机卫士,安全狗,云锁之类的软waf进行拦截,经常碰到如下拦截提示: 看到以上三个拦截提示就让人头疼不已,欲罢不能. so ...
- Java Unsafe类
参考了这篇文章:http://blog.csdn.net/aesop_wubo/article/details/7537278 <JAVA并发编程学习笔记之Unsafe类> Unsafe开 ...
- [MFC]选择目录对话框和选择文件对话框 [转]
在MFC编程中经常会需要用到选择目录和选择文件的界面,以下总结一下本人常用的这两种对话框的生成方法: 选择目录对话框 { char szPath[MAX_PATH]; //存放选择的 ...