ASP.NET MVC4 Razor模板简易分页效果
网络编程 2021-07-04 22:41www.168986.cn编程入门
这篇文章主要为大家详细介绍了ASP.NET MVC4 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>(); s.Add("张军"); ViewBag.PageCount = (int)Math.Ceiling(s.Count() / 20.0); return View(s.Skip((pageInd - 1) 20).Take(20)); @Html.Action("PageIndex", "PageIndex", new { action = "", controller = "", pageCount = ViewBag.PageCount, currentPage = ViewBag.CurrentPage })
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持狼蚁SEO。
编程语言
- 如何快速学会编程 如何快速学会ug编程
- 免费学编程的app 推荐12个免费学编程的好网站
- 电脑怎么编程:电脑怎么编程网咯游戏菜单图标
- 如何写代码新手教学 如何写代码新手教学手机
- 基础编程入门教程视频 基础编程入门教程视频华
- 编程演示:编程演示浦丰投针过程
- 乐高编程加盟 乐高积木编程加盟
- 跟我学plc编程 plc编程自学入门视频教程
- ug编程成航林总 ug编程实战视频
- 孩子学编程的好处和坏处
- 初学者学编程该从哪里开始 新手学编程从哪里入
- 慢走丝编程 慢走丝编程难学吗
- 国内十强少儿编程机构 中国少儿编程机构十强有
- 成人计算机速成培训班 成人计算机速成培训班办
- 孩子学编程网上课程哪家好 儿童学编程比较好的
- 代码编程教学入门软件 代码编程教程