zoukankan      html  css  js  c++  java
  • MVC下实现分页

    菜鸟第一天                               

           作为菜鸟,记录以后可能会使用的到技术会使自己的未来更加轻松。。。。。。。。。。

           今天学习了在MVC下实现非插件式分页效果,通过创建一个分页方法,可以实现在任何需要进行分页的MVC页面上进行分页,直接在MVC页面上调用即可,废话不多说,直接上代码:

        分页的实现代码:       

     1 //返回一个拼接后的MvcHtmlString 字符串输出
     2 //this HtmlHelper pagehelper表示为MVC中的HTML添加拓展方法
     3 //area是在项目中创建了一个区域文件夹
     4 //action是函数名
     5 //controller是控制器
     6 //pagesize是html页面中显示的数量
     7 //totalcount是数据库中总数据的行数
     8 public static MvcHtmlString PageBarBulider(this HtmlHelper pagehelper, string area, string action, string controller, int pagesize, int totalcount)
     9 {
    10 //从url参数pageindex获取当前的页索引
    11 string currentpageindex = HttpContext.Current.Request.QueryString["pageindex"];
    12 int icurrentPi = 1;
    13 //由于第一次进入列表页面不会带有pageindex和pagesize,所以要赋值
    14 //当为空的时候赋值1,表示第一次打开时默认获取第一页
    15 if (string.IsNullOrEmpty(currentpageindex))
    16 {
    17 icurrentPi = 1;
    18 }
    19 else
    20 {
    21 icurrentPi = Convert.ToInt32(currentpageindex);
    22 }
    23 //通过天花板函数根据传过来的totalcount算出有多少页数据
    24 decimal pagecount = Math.Ceiling(totalcount / Convert.ToDecimal(pagesize));
    25 
    26 
    27 //1.0拼接当前分页控件使用的url,判断是否有区域
    28 string areaPix = string.IsNullOrEmpty(area) ? "" : "/"+area;
    29 string urlfmt = areaPix + "/" + controller + "/" + action + "?pageindex={0}&pagesize={1}";
    30 
    31 
    32 StringBuilder sbhtml = new StringBuilder(200);
    33 sbhtml.Append("<div id=pagebar><ul>");
    34 sbhtml.Append("<li><a href=""+string.Format(urlfmt,1,pagesize)+"">首页</a></li>");
    35 sbhtml.Append("<li><a href=""+string.Format(urlfmt,icurrentPi==1?1:icurrentPi-1,pagesize)+"">上一页</a></li>");
    36 for (int i = 1; i <=pagecount; i++)
    37 {
    38 //如果当前选中,那么添加背景色
    39 if (i == icurrentPi)
    40 {
    41 sbhtml.Append("<li><a style='color:#ff6a00;' href=""+string.Format(urlfmt,i,pagesize)+"">"+i+"<a></li>");
    42 }
    43 else
    44 {
    45 sbhtml.Append("<li><a href="" + string.Format(urlfmt, i, pagesize) + "">" + i + "<a></li>");
    46 }
    47 }
    48 sbhtml.Append("<li><a href=""+string.Format(urlfmt,icurrentPi==pagecount?pagecount:icurrentPi+1,pagesize)+"">下一页</a></li>");
    49 sbhtml.Append("<li><a href=""+string.Format(urlfmt,pagecount,pagesize)+"">尾页</a></li>");
    50 sbhtml.Append("</ul></div>");
    51 //将拼接好的sbhtml转为字符串形式返回到Razor视图引擎解析
    52 return new MvcHtmlString(sbhtml.ToString());

     上面写好了获取分页数据的方法,那么现在在某一个任意Razor视图上调用该方法,因为是HTML的拓展方法,那么可以在一个表格后面通过HTML.PageBarBulider(输入5个参数),具体如下:

      在上面调用后在第一次调用会出错的,因为View.totalcount还没有从控制器后台返回来,并且在第一次打开页面时要默认分页,所以在后台处理时,我们要先获取数据库中的总行数,同时还要判断如果是第一次打开则默认加载第一页数据,同时,因为在点击上一页和下一页的时候,已把url拼接成格式如下:

    sbhtml.Append("<li><a href=""+string.Format(urlfmt,icurrentPi==1?1:icurrentPi-1,pagesize)+"">上一页</a></li>")类似于/Area/Controller/Action/pageindex=1&pagesize=5

    所以控制器后台代码同时也要获取点击上一页或下一页的URL,根据URL后面的传递的指定参数从数据库中获取指定的数据,控制器中代码如下:

     1   public ActionResult Index()
     2         {
     3             //获取数据库中的总数据行,传给视图
     4             ViewBag.totalcount = categoryBLL.DbSet.Count();
     5 
     6             //1.0从url获取相关参数
     7             string pageindex = Request.QueryString["pageindex"];
     8             string pagesize = Request.QueryString["pagesize"];
     9             //2.0初始化页码和每页显示的数据
    10             int ipageindex;
    11             int ipagesize;
    12             if (int.TryParse(pageindex, out ipageindex) == false)
    13             {
    14                 //如果转换不成功,那么就赋值为1,表示当前的第一页
    15                 ipageindex = 1;
    16             }
    17             if (int.TryParse(pagesize, out ipagesize) == false)
    18             {
    19                 ipagesize = 5;
    20             }
    21             //3.0算出当前分页应该跳过的数据行数
    22             int skipcount = (ipageindex - 1) * ipagesize;
    23 
    24             //4.0通过传递过来的参数获取数据库中指定行数据
    25             var list = base.categoryBLL.DbSet.OrderByDescending(c => c.c_id).Skip(skipcount).Take(ipagesize).ToList();
    26 
    27             //5.0EntityMap()方法是将MVC中的自定义的实体转为EF自动生成的实体,同样的,可以通过new匿名来进行转换
    28             var listmodeview = list.Select(c => c.EntityMap());
    29 
    30             return View(listmodeview);
    31         }

      第一次写博客,有点紧张,有错请指点,不喜勿喷!

     

  • 相关阅读:
    推介一款小工具——SwitchHosts
    Postman的使用之进行文件上传
    Postman的使用之普通的提交Post和Get请求
    Postman的安装
    Windows配置基础环境(jdk+tomcat)
    yum clean all大坑解决
    通过代理实现访问内网系统
    批量配置免密登录
    设置JRebel热部署【本地模式】
    使用多线程程序模拟实现单生产者/多消费者问题 (Linux 线程锁实践)
  • 原文地址:https://www.cnblogs.com/jean69/p/3629706.html
Copyright © 2011-2022 走看看