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的模块化/插件式架构实现 概述 在日常开发中, 我们经常谈起模块化/插件化架构,这样可既可以提高开效率,又可以实现良好的扩展性,尤其对于产品化的系统有 ...
随机推荐
- java 如何读取 properties 配置文件
- [转]实际项目中如何使用Git做分支管理 (A successful Git branching model)
来自 https://nvie.com/posts/a-successful-git-branching-model/ In this post I present the development m ...
- Educational Codeforces Round 42 (Rated for Div. 2) E. Byteland, Berland and Disputed Cities(贪心)
E. Byteland, Berland and Disputed Cities time limit per test2 seconds memory limit per test256 megab ...
- robots.txt防止向黑客泄露网站的后台和隐私
为了不让搜索引擎索引网站的后台页面或其它隐私页面,我们将这些路径在robots.txt文件中禁用了.但矛盾的是,robots.txt文件任何人都可以访问,包括黑客.为了禁止搜索引擎,我们把隐私泄露给了 ...
- BZOJ - 3998 弦论 (后缀自动机)
#include<cstdio> #include<cstring> #include<queue> using namespace std; typedef lo ...
- ZROI 19.08.01 生成函数方法
写在前面:由于我数学基础不好,加上缺乏生成函数知识,所以这一下午我都处在掉线和非掉线的叠加态.而且我写\(\LaTeX\)很慢,所以笔记相当混乱而且不全面.说白了就是我太菜了听不懂. 1.一般生成函数 ...
- vue history模式下的微信分享
// 微信验证 export function requireConfig() { let url = window.location.href systemApi.wxoption({ url: u ...
- 【NOIP2016提高A组模拟9.24】我的快乐时代
题目 分析 虽然我们很难求出\(\sum_{i=n}^mjoy(i)\), 但是我们可以分别求出\(\sum_{i=1}^mjoy(i)\)和\(\sum_{i=1}^{n-1}joy(i)\),相减 ...
- 记第一次正式线上笔试(Tencent——正式考-技术研发类-综合-2018实习生招聘)
选择题做的跟傻逼一样,不多说了..大学只打了ACM还不是计算机科班出身的我,连好多名词都不认识..... 三道编程题很简单,下面给出三道题的大致题意以及题解. 1.给出n和m,满足(2m)可以整除n. ...
- 安装caffe碰到的坑(各种.so未找到)
./include/caffe/common.hpp:4:32: fatal error: boost/shared_ptr.hpp: 没有那个文件或目录 所有类似于上面的错误,都可以用如下格式来解决 ...