欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

ASP.NET MVC4 Razor模板简易分页效果

程序员文章站 2023-11-30 08:23:28
一、无数据提交 第一步,建立一个 controller命名为pageindex的空控制器,自定义一个方法如下:   ...

一、无数据提交

第一步,建立一个 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 })

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