ASP.NET MVC4 Razor模板简易分页效果
一、无数据提交
第一步,建立一个Controller命名为PageIndex的空控制器,自定义一个方法如下:
publicActionResultPageIndex(stringaction,stringcontroller,intcurrentPage,intpageCount) { //intcount=db.Product.Count(); ViewBag.PageCount=pageCount;//从操作中获取总数据页数将传入分页视图页面 ViewBag.CurrentPage=currentPage;//从操作中获取当前页数将传入分页视图页面 ViewBag.action=action; ViewBag.controller=controller; returnPartialView(); }
传入四个参数:
action:操作(要分页的视图的操作,默认为Index);
controller:控制器;
currentPage:当前页数;
pageCount:数据总页数
第二步:添加视图(PageIndex)
@if(ViewBag.PageCount==null||ViewBag.PageCount==0) { <span>您好,当前没有数据显示!</span> } else { if(ViewBag.CurrentPage<=10) { <span><ahref="@Url.Action(ViewBag.action,ViewBag.controller,new{PageIndex=1},null)"> 首页</a>|</span> } else { <ahref="@Url.Action(ViewBag.action,ViewBag.controller,new{PageIndex=1},null)"> 首页</a> <span><ahref="@Url.Action(ViewBag.action,ViewBag.controller,new{PageIndex=ViewBag.CurrentPage-10},null)"> ...</a></span> } for(inti=ViewBag.CurrentPage-3;i<ViewBag.CurrentPage+3;i++) { if(i<=0) { continue; } if(i>ViewBag.PageCount) { break; } <span><ahref="@Url.Action(ViewBag.action,ViewBag.controller,new{PageIndex=i},null)"> 第@i页</a>|</span> } if(ViewBag.CurrentPage>1) { <span><ahref="@Url.Action(ViewBag.action,ViewBag.controller,new{PageIndex=ViewBag.CurrentPage-1},null)"> 上一页</a>|</span> } if(ViewBag.PageCount>ViewBag.CurrentPage) { <span><ahref="@Url.Action(ViewBag.action,ViewBag.controller,new{PageIndex=ViewBag.CurrentPage+1},null)"> 下一页</a></span> } if(ViewBag.CurrentPage==ViewBag.PageCount||ViewBag.CurrentPage>=ViewBag.PageCount-10) { <ahref="@Url.Action(ViewBag.action,ViewBag.controller,new{PageIndex=ViewBag.PageCount},null)"> 尾页</a> } else { <span><ahref="@Url.Action(ViewBag.action,ViewBag.controller,new{PageIndex=ViewBag.CurrentPage+10},null)"> ...</a></span> <ahref="@Url.Action(ViewBag.action,ViewBag.controller,new{PageIndex=ViewBag.PageCount},null)"> 尾页</a> } <spanstyle="padding-left:20px">当前页数:@ViewBag.CurrentPage|共@ViewBag.PageCount页 </span> }
第三步:操作的视图的控制器修改
publicViewResultIndex(int?pageIndex) { intpageInd=pageIndex.HasValue?pageIndex.Value:1; ViewBag.PageCount=(int)Math.Ceiling(result.Count()/20.0); //这里的是take,按照每页20个显示 returnView(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的空控制器,自定义一个方法如下:
publicActionResultPageIndexKey(intcurrentPage,intpageCount) { ViewBag.PageCount=pageCount;//从操作中获取总数据页数将传入分页视图页面 ViewBag.CurrentPage=currentPage;//从操作中获取当前页数将传入分页视图页面 returnPartialView(); }
第二步:建立分布视图
<script> $(function(){ $("#pageingByForma").click(function(event){ $("#pageIndex").val($(this).attr("pageIndex")); //$(this).parent("Form").submit(); document.getElementsByTagName("Form").item(0).submit(); event.preventDefault(); }); }); </script> @Html.Hidden("pageIndex") <divid="pageingByForm"> @if(ViewBag.PageCount==null||ViewBag.PageCount==0) { <span>当前没有数据</span> } else { if(ViewBag.CurrentPage<=10) { <span><apageindex="1"href="#">首页</a>|</span> } else { <span><apageindex="1"href="#">首页</a>|</span> <span><apageIndex="@(ViewBag.CurrentPage-10)"href="#">...</a>|</span> } for(inti=ViewBag.CurrentPage-3;i<ViewBag.CurrentPage+3;i++) { if(i<=0) { continue; } if(i>ViewBag.PageCount) { break; } <span><apageIndex="@i"href="#">第@i页</a>|</span> } if(ViewBag.CurrentPage>1) { <span><apageIndex="@(ViewBag.CurrentPage-1)"href="#">上一页</a>|</span> } if(ViewBag.PageCount>ViewBag.CurrentPage) { <span><apageIndex="@(ViewBag.CurrentPage+1)"href="#">下一页</a></span> } if(ViewBag.CurrentPage>=ViewBag.PageCount-10) { } else { <span><apageIndex="@(ViewBag.CurrentPage+10)"href="#">...</a>|</span> <span><apageIndex="@ViewBag.PageCount"href="#">尾页</a></span> } <spanstyle="padding-left:20px">当前页数:@ViewBag.CurrentPage|共@ViewBag.PageCount页 </span> } </div>
第三步:修改操作视图和控制器
publicViewResultIndex(int?pageIndex,stringsearch) { intpageInd=pageIndex.HasValue?pageIndex.Value:1; ViewBag.PageCount=(int)Math.Ceiling(result.Count()/20.0); returnView(result.OrderBy(t=>t.PID).Skip((pageInd-1)*20).Take(20)); }
视图(页面调用):
@using(Html.BeginForm())
{
根据性别得到查询结果
性别:@Html.TextBox("sex")
<inputtype="submit"value="查询"/>
@Html.Action("PageIndexKey","PageIndex",new{pageCount=ViewBag.PageCount,currentPage=ViewBag.CurrentPage})
}
Example:
//数据,一个list的集合 List<string>s=newList<string>(); s.Add("张军"); ViewBag.PageCount=(int)Math.Ceiling(s.Count()/20.0); returnView(s.Skip((pageInd-1)*20).Take(20)); @Html.Action("PageIndex","PageIndex", new{action="",controller="",pageCount=ViewBag.PageCount,currentPage=ViewBag.CurrentPage})
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。