相思资源网 Design By www.200059.com

一、无数据提交

第一步,建立一个 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"codetitle">复制代码 代码如下:@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"color: #800000"> @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 })

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

标签:
ASP.NET,MVC4,Razor,分页

相思资源网 Design By www.200059.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
相思资源网 Design By www.200059.com

评论“ASP.NET MVC4 Razor模板简易分页效果”

暂无ASP.NET MVC4 Razor模板简易分页效果的评论...

稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!

昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。

这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。

而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?