富贵资源网 Design By www.hznty.com

如图:

MVC异步分页代码分享 

1、控制器代码 

//

    // GET: /AjaxUser/

    shopEntities shop = new shopEntities();
    public ActionResult Index()
    {
      return View();
    }
    public ActionResult loadjson()

    {
      int pageSize = Request["pageSize"] == null "pageSize"]);
      int pageIndex = Request["pageIndex"] == null "pageIndex"]);
      int totalCount = shop.tbl_admin.Count();
 

      //给前台userinfo所有的数据,并且是json格式
      var allorder = shop.tbl_admin.OrderBy(u=>u.id)
        .Skip(pageSize*(pageIndex-1))
        .Take(pageSize)
        .ToList();
      //接受一个对像,内部把此对象使用javaScript序列化类对象志字符串,发送到前台
      var data = from u in allorder select new { u.id,u.realname,u.sex};
      string strNav = PageNavHelper.ShowPageNavigate(pageIndex,pageSize,totalCount);
      var result = new {Data=data, NavStr=strNav };
      return Json(result, JsonRequestBehavior.AllowGet);

    }

2、Html代码 

@{
  Layout = null;
}
<!DOCTYPE html>
<html>

<head>

  <meta name="viewport" content="width=device-width" />

  <title>Index</title>
  <link href="~/Content/NavPage.css" rel="stylesheet" />
  <script src="/UploadFiles/2021-04-02/jquery-1.8.2.min.js">

3、分页类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
namespace MvcTest4.Models

{
  public class PageNavHelper

  {
    //主要就是输出分页的超级链接的标签

    //自定义分页Helper扩展

    public static string ShowPageNavigate(int currentPage, int pageSize, int totalCount)
    {

      var redirectTo = HttpContext.Current.Request.Url.AbsolutePath;
      pageSize = pageSize <= 0 "<a class='pageLink' href='{0}", redirectTo, pageSize);
        }
        if (currentPage > 1)
        {//处理上一页的连接
          output.AppendFormat("<a class='pageLink' href='{0}", redirectTo, currentPage - 1, pageSize);
        }

        else
        {
          // output.Append("<span class='pageLink'>上一页</span>");

        }
        output.Append(" ");
        int currint = 5;
        for (int i = 0; i <= 10; i++)
        {//一共最多显示10个页码,前面5个,后面5个
          if ((currentPage + i - currint) >= 1 && (currentPage + i - currint) <= totalPages)
          {

            if (currint == i)

            {//当前页处理

              //output.Append(string.Format("[{0}]", currentPage));

              output.AppendFormat("<a class='cpb' href='{0}", redirectTo, currentPage, pageSize, currentPage);
            }
            else
            {//一般页处理
              output.AppendFormat("<a class='pageLink' href='{0}", redirectTo, currentPage + i - currint, pageSize, currentPage + i - currint);

            }
          }
          output.Append(" ");

        }

        if (currentPage < totalPages)
        {//处理下一页的链接
          output.AppendFormat("<a class='pageLink' href='{0}", redirectTo, currentPage + 1, pageSize);
        }
        else
        {
          //output.Append("<span class='pageLink'>下一页</span>");
        }
        output.Append(" ");
        if (currentPage != totalPages)
        {
          output.AppendFormat("<a class='pageLink' href='{0}", redirectTo, totalPages, pageSize);
        }
        output.Append(" ");
      }
      output.AppendFormat("第{0}页 / 共{1}页", currentPage, totalPages);//这个统计加不加都行
 
      return output.ToString();

    }

  }

} 

4、分页CSS 

body {
} 
.paginator {
  font: 12px Arial, Helvetica, sans-serif;
  padding: 10px 20px 10px 0;
  margin: 0px;
}

  .paginator a {
    border: solid 1px #ccc;
    color: #0063dc;
    cursor: pointer;
    text-decoration: none;
  }
    .paginator a:visited {
      padding: 1px 6px;
      border: solid 1px #ddd;
      background: #fff;
      text-decoration: none;
    }
  .paginator .cpb {
    border: 1px solid #F50;
    font-weight: 700;
    color: #F50;
    background-color: #ffeee5;
  }

  .paginator a:hover {
    border: solid 1px #F50;
    color: #f60;
    text-decoration: none;
  }
  .paginator a, .paginator a:visited, .paginator .cpb, .paginator a:hover {
    float: left;
    height: 16px;
    line-height: 16px;
    min-width: 10px;
    _width: 10px;
    margin-right: 5px;
    text-align: center;
    white-space: nowrap;
    font-size: 12px;
    font-family: Arial,SimSun;
    padding: 0 3px;

  } 

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

富贵资源网 Design By www.hznty.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
富贵资源网 Design By www.hznty.com