当前位置: 移动技术网 > IT编程>开发语言>.net > MVC默认路由实现分页(PagerExtend.dll下载)

MVC默认路由实现分页(PagerExtend.dll下载)

2017年12月12日  | 移动技术网IT编程  | 我要评论

谁能百里挑一李玉婵,131458信誉查询,豪门长媳十八岁

这两天在群里有人咨询有没有现成的.net mvc分页方法,由此写了一个简单分页工具,这里简单分享下实现思路,代码,希望能对大家有些帮助,鼓励大家多造些轮子还是好的。

a.效果(这里用了bootstrap的样式)

b.分析,知识点

a.分页通常由一下几个属性组成(当前页,总条数,分页记录数,路由地址),由此四项基本就能实现分页了,在加上一个控制样式的参数

b.各种数字的验证,计算总页数(如果总条数和分页记录数不能整除,那么最后相除的结果再+1)

c.下一页和上一下的按钮是零界点,需要判断是否是最后一页或者第一页来显示当前页数的继续增加或者减小

d.因为需要在cshtml文件中展示分页的效果,所以需要用到htmlhelper扩展方法;扩展方法这里简单说下注意项:

.关键词this

.扩展方法对应的clas必须静态,该方法本身也是静态

.扩展方法对应的class后缀一般是extensions修饰

e.试图页面@html.pageextend直接调用分页方法

c.代码展示

a.分页方法实现类

using system;
using system.collections.generic;
using system.linq;
using system.text;
using system.threading.tasks;
using system.web.mvc;

namespace pagerextend
{
 public static class htmlhelperextensions
 {

  #region 分页扩展 pageextend

  /// <summary>
  /// 分页option属性
  /// </summary>
  public class mopageroption
  {
   /// <summary>
   /// 当前页 必传
   /// </summary>
   public int currentpage { get; set; }
   /// <summary>
   /// 总条数 必传
   /// </summary>
   public int total { get; set; }

   /// <summary>
   /// 分页记录数(每页条数 默认每页15条)
   /// </summary>
   public int pagesize { get; set; }

   /// <summary>
   /// 路由地址(格式如:/controller/action) 默认自动获取
   /// </summary>
   public string routeurl { get; set; }

   /// <summary>
   /// 样式 默认 bootstrap样式 1
   /// </summary>
   public int stylenum { get; set; }
  }

  /// <summary>
  /// 分页扩展方法
  /// </summary>
  /// <param name="helper">html试图</param>
  /// <param name="option">分页属性</param>
  /// <returns>html样式</returns>
  public static mvchtmlstring pageextend(this htmlhelper helper, mopageroption option)
  {

   if (option.pagesize <= 0) { option.pagesize = 15; }
   if (option.currentpage <= 0) { option.currentpage = 1; }
   if (option.total <= 0) { return mvchtmlstring.empty; }

   //总页数
   var totalpage = option.total / option.pagesize + (option.total % option.pagesize > 0 ? 1 : 0);
   if (totalpage <= 0) { return mvchtmlstring.create("分页异常"); }
   //当前路由地址
   if (string.isnullorempty(option.routeurl))
   {

    option.routeurl = helper.viewcontext.httpcontext.request.rawurl;
    if (!string.isnullorempty(option.routeurl))
    {

     var lastindex = option.routeurl.lastindexof("/");
     option.routeurl = option.routeurl.substring(0, lastindex);
    }
   }
   option.routeurl = option.routeurl.trimend('/');

   //构造分页样式
   var sbpage = new stringbuilder(string.empty);
   switch (option.stylenum)
   {
    case 2:
     {
      break;
     }
    default:
     {
      #region 默认样式

      sbpage.append("<nav>");
      sbpage.append(" <ul class=\"pagination\">");
      sbpage.appendformat("  <li><a href=\"{0}/{1}\" aria-label=\"previous\"><span aria-hidden=\"true\">«</span></a></li>",
            option.routeurl,
            option.currentpage - 1 <= 0 ? 1 : option.currentpage - 1);

      for (int i = 1; i <= totalpage; i++)
      {

       sbpage.appendformat("  <li {1}><a href=\"{2}/{0}\">{0}</a></li>",
        i,
        i == option.currentpage ? "class=\"active\"" : "",
        option.routeurl);

      }

      sbpage.append("  <li>");
      sbpage.appendformat("   <a href=\"{0}/{1}\" aria-label=\"next\">",
           option.routeurl,
           option.currentpage + 1 > totalpage ? option.currentpage : option.currentpage + 1);
      sbpage.append("    <span aria-hidden=\"true\">»</span>");
      sbpage.append("   </a>");
      sbpage.append("  </li>");
      sbpage.append(" </ul>");
      sbpage.append("</nav>");
      #endregion
     }
     break;
   }


   return mvchtmlstring.create(sbpage.tostring());
  }
  #endregion
 }
}

b.view测试调用

@using pagerextend
@model ienumerable<xinsheng.api.controllers.moairticle>

<table>
 url:@viewbag.url

 @foreach (var item in model)
 {
  <tr>
   <td>@item.title</td>
   <td>@item.author</td>
   <td>@item.createtime</td>
  </tr>
 }
</table>

@html.pageextend(viewbag.pageroption as htmlhelperextensions.mopageroption)

c.controller测试

using pagerextend;
using system;
using system.collections.generic;
using system.linq;
using system.web;
using system.web.mvc;
using system.web.security;

namespace xinsheng.api.controllers
{

 [serializable]
 public class moairticle
 {

  public string title { get; set; }
  public string author { get; set; }
  public datetime createtime { get; set; }
 }

 public class homecontroller : controller
 {

  public actionresult index(int id)
  {
   viewbag.title = "测试 分页";

   list<moairticle> moairticles = new list<moairticle>();

   for (int i = 1; i < 50; i++)
   {

    moairticles.add(new moairticle
    {
     author = "神牛步行" + i,
     createtime = datetime.now,
     title = "博客园之" + i
    });
   }
   viewbag.url = request.rawurl;

   //初始化分页基础信息
   var option = new htmlhelperextensions.mopageroption
   {

    currentpage = id,
    pagesize = 15,
    total = moairticles.count
   };
   //动态传递分页属性
   viewbag.pageroption = option;

   var articles = moairticles.skip((option.currentpage - 1) * option.pagesize).take(option.pagesize).tolist();
   return view(articles);
  }
 }
}

d.分页pagerextend.dll下载地址:

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网