MVC简易分页(Razor)
一、无数据提交
第一步,建立一个 Controller命名为PageIndex的空控制器,自定义一个方法如下:
public ActionResult PageIndex(string action, string controller, int currentPage, int pageCount)
{
//int count = db.Product.Count();
ViewBag.PageCount = pageCount;//从操作中获取总数据页数将传入分页视图页面
ViewBag.CurrentPage = currentPage;//从操作中获取当前页数将传入分页视图页面
ViewBag.action = action;
ViewBag.controller = controller;
return PartialView();
}
传入四个参数:
action:操作(要分页的视图的操作,默认为Index);
controller:控制器;
currentPage:当前页数;
pageCount:数据总页数
第二步:添加视图(PageIndex)
@if (ViewBag.PageCount == null || ViewBag.PageCount == 0)
{
<span>您好,当前没有数据显示!</span>
}
else
{
if (ViewBag.CurrentPage <= 10)
{
<span><a href="@Url.Action(ViewBag.action, ViewBag.controller, new { PageIndex = 1 }, null)">
首页</a>|</span>
}
else
{
<a href="@Url.Action(ViewBag.action, ViewBag.controller, new { PageIndex = 1 }, null)">
首页</a>
<span><a href="@Url.Action(ViewBag.action, ViewBag.controller, new { PageIndex = ViewBag.CurrentPage - 10 }, null)">
...</a> </span>
}
for (int i = ViewBag.CurrentPage - 3; i < ViewBag.CurrentPage + 3; i++)
{
if (i <= 0)
{
continue;
}
if (i > ViewBag.PageCount)
{
break;
}
<span><a href="@Url.Action(ViewBag.action, ViewBag.controller, new { PageIndex = i }, null)">
第 @i 页</a>|</span>
}
if (ViewBag.CurrentPage > 1)
{
<span><a href="@Url.Action(ViewBag.action, ViewBag.controller, new { PageIndex = ViewBag.CurrentPage - 1 }, null)">
上一页</a>|</span>
}
if (ViewBag.PageCount > ViewBag.CurrentPage)
{
<span><a href="@Url.Action(ViewBag.action, ViewBag.controller, new { PageIndex = ViewBag.CurrentPage + 1 }, null)">
下一页</a></span>
}
if (ViewBag.CurrentPage == ViewBag.PageCount || ViewBag.CurrentPage >= ViewBag.PageCount - 10)
{
<a href="@Url.Action(ViewBag.action, ViewBag.controller, new { PageIndex = ViewBag.PageCount }, null)">
尾 页</a>
}
else
{
<span><a href="@Url.Action(ViewBag.action, ViewBag.controller, new { PageIndex = ViewBag.CurrentPage + 10 }, null)">
...</a></span>
<a href="@Url.Action(ViewBag.action, ViewBag.controller, new { PageIndex = ViewBag.PageCount }, null)">
尾 页</a>
}
<span style="padding-left: 20px">当前页数: @ViewBag.CurrentPage | 共 @ViewBag.PageCount 页
</span>
}
第三步:操作的视图的控制器修改
public ViewResult Index(int? pageIndex)
{
int pageInd = pageIndex.HasValue ? pageIndex.Value : 1;
ViewBag.PageCount = (int)Math.Ceiling(result.Count() / 20.0);
//这里的是take,按照每页20个显示
return View(result.OrderBy(t => t.PID).Skip((pageInd - 1) * 20).Take(20));
}
第四步:页面调用(即最后一步)
@Html.Action("PageIndex", "Product", new { action = "Index", controller = "Log", pageCount = ViewBag.PageCount, currentPage = ViewBag.CurrentPage })
一般来说,数据都是变动的。
二、有数据提交
第一步:建立一个 Controller命名为PageIndex的空控制器,自定义一个方法如下:
public ActionResult PageIndexKey(int currentPage, int pageCount)
{
ViewBag.PageCount = pageCount;//从操作中获取总数据页数将传入分页视图页面
ViewBag.CurrentPage = currentPage;//从操作中获取当前页数将传入分页视图页面
return PartialView();
}
第二步:建立分布视图
<script>
$(function () {
$("#pageingByForm a").click(function (event) {
$("#pageIndex").val($(this).attr("pageIndex"));
//$(this).parent("Form").submit();
document.getElementsByTagName("Form").item(0).submit();
event.preventDefault();
});
});
</script>
@Html.Hidden("pageIndex")
<div id="pageingByForm">
@if (ViewBag.PageCount == null || ViewBag.PageCount == 0)
{
<span>当前没有数据</span>
}
else
{
if (ViewBag.CurrentPage <= 10)
{
<span><a pageindex="1" href="#">首页</a>|</span>
}
else
{
<span><a pageindex="1" href="#">首页</a>|</span>
<span><a pageIndex="@(ViewBag.CurrentPage - 10)" href="#">...</a>|</span>
}
for (int i = ViewBag.CurrentPage - 3; i < ViewBag.CurrentPage + 3; i++)
{
if (i <= 0)
{
continue;
}
if (i > ViewBag.PageCount)
{
break;
}
<span><a pageIndex="@i" href="#">第 @i 页</a>|</span>
}
if (ViewBag.CurrentPage >1)
{
<span><a pageIndex="@(ViewBag.CurrentPage - 1)" href="#">上一页</a>|</span>
}
if (ViewBag.PageCount > ViewBag.CurrentPage)
{
<span><a pageIndex="@(ViewBag.CurrentPage + 1)" href="#">下一页</a></span>
}
if (ViewBag.CurrentPage >= ViewBag.PageCount - 10)
{
}
else
{
<span><a pageIndex="@(ViewBag.CurrentPage + 10)" href="#">...</a>|</span>
<span><a pageIndex="@ViewBag.PageCount" href="#">尾 页</a></span>
}
<span style="padding-left: 20px">当前页数: @ViewBag.CurrentPage | 共 @ViewBag.PageCount 页
</span>
}
</div>
第三步:修改操作视图和控制器
public ViewResult Index(int? pageIndex ,string search)
{
int pageInd = pageIndex.HasValue ? pageIndex.Value : 1;
ViewBag.PageCount = (int)Math.Ceiling(result.Count() / 20.0);
return View(result.OrderBy(t => t.PID).Skip((pageInd - 1) * 20).Take(20));
}
视图(页面调用):
@using (Html.BeginForm())
{
根据性别得到查询结果
性别: @Html.TextBox("sex")
<input type="submit" value="查询" />
@Html.Action("PageIndexKey", "PageIndex", new { pageCount = ViewBag.PageCount, currentPage = ViewBag.CurrentPage })
}
Example:
//数据,一个list的集合
List<string> s = new List<string>();
@Html.Action("PageIndex", "PageIndex",
new { action = "", controller = "", pageCount = ViewBag.PageCount, currentPage = ViewBag.CurrentPage })
MVC简易分页(Razor)的更多相关文章
- (转)Asp.Net Mvc视图引擎Razor介绍
Asp.Net Mvc视图引擎Razor介绍 1.Razor介绍 程序园原创,转载请注明:http://www.kwstu.com/ArticleView/dabaomvc_2014082408205 ...
- 基于存储过程的MVC开源分页控件--LYB.NET.SPPager
摘要 现在基于ASP.NET MVC的分页控件我想大家都不陌生了,百度一下一大箩筐.其中有不少精品,陕北吴旗娃杨涛大哥做的分页控件MVCPager(http://www.webdiyer.com/)算 ...
- Mvc自定义分页控件
MVC开发分页常常使用第三方控件,生成的分页HTML带有版权申明,虽然免费,但是总有的别扭.于是,某日,楼主闲来蛋疼,折腾了个自定义分页控件: 先来展示下效果图: 1>当分页不超过10页的时候, ...
- MVC简单分页
对Car汽车表分页 实现简单分页,放在这里方便查看回顾,自定义每页几条有点问题,有待完善······ 1.新建mvc项目 2.添加linq to sql 数据库连接 3.添加CarBF类 using ...
- MVC快速分页
.NET手记-ASP.NET MVC快速分页的实现 对于Web应用,展示List是很常见的需求,随之而来的常见的分页组件.jQuery有现成的分页组件,网上也有着大量的第三方分页组件,都能够快速实 ...
- MVC自定义分页
MVC自定义分页 之前我发表了一篇MVC无刷新分页的文章,里面用的是MvcPager控件,但是那个受那个控件限制,传值只能用PagedList,各方面都受到了限制,自由度不够高,现在还是做MVC无刷新 ...
- 基于存储过程的MVC开源分页控件
基于存储过程的MVC开源分页控件--LYB.NET.SPPager 摘要 现在基于ASP.NET MVC的分页控件我想大家都不陌生了,百度一下一大箩筐.其中有不少精品,陕北吴旗娃杨涛大哥做的分页控件M ...
- 学习ASP.NET MVC(十一)——分页
在这一篇文章中,我们将学习如何在MVC页面中实现分页的方法.分页功能是一个非常实用,常用的功能,当数据量过多的时候,必然要使用分页.在今天这篇文章中,我们学习如果在MVC页面中使用PagedList. ...
- 转发-基于ASP.NET MVC 4/5 Razor的模块化/插件式架构实现
基于ASP.NET MVC 4/5 Razor的模块化/插件式架构实现 概述 在日常开发中, 我们经常谈起模块化/插件化架构,这样可既可以提高开效率,又可以实现良好的扩展性,尤其对于产品化的系统有 ...
随机推荐
- kafka复习(1)
一:flume复习 0.JMS(java message service )java消息服务 ----------------------------------------------------- ...
- 移除数组中指定键(Yii2)
/** * 移除数组中指定key * @param $data * @param $key * @return array */ public static function removeKey($d ...
- mybatis返回map结果集
今天突发奇想,想用mybatis返回一个map结果集,结果我就整了一下午,不过终于解决了 1.如果你确定返回的数据只有一条,你可以这样整 xml中: <select id="searc ...
- Delphi简介
- hive模拟数据
人员表 id,姓名,爱好,住址 1,小明1,lol-book-movie,beijing:mashibing-shanghai:pudong 2,小明2,lol-book-movie,beijing: ...
- svn 介绍及linux下常用操作命令
1.概念 truck(主干|主线|主分支):是用来做主方向开发的,新功能的开发应放在主线中,当模块开发完成后,需要修改,就用branch. branch(分支):分支开发和主线开发是可以同时进行的,也 ...
- pikachu-xss和csrf
简介 XSS是一种发生在Web前端的漏洞,所以其危害的对象也主要是前端用户 XSS漏洞可以用来进行钓鱼攻击.前端js挖矿.盗取用户cookie,甚至对主机进行远程控制 攻击流程 假设存在漏洞的是一个论 ...
- SOA,Webservice,SOAP,REST,RPC,RMI,JMS的区别与联系(转载)
原文地址:http://blog.csdn.net/pcceo1/article/details/51245249 SOA面向服务的软件架构(Service Oriented Architecture ...
- Spring MVC使用AOP实现审计日志
先定一个注解,用于在Controller方法上记录每个方法的用途. package com.zjf.spring.annotation; import java.lang.annotation.Doc ...
- 【Linux】CentOS6上redis安装
1.官网下载安装包 https://redis.io 2.解压 tar -zxvf xxxx.tar.gz 3.编译安装 进入解压后的目录后 make 出现以下内容表示make成功 Hint: It' ...