zoukankan      html  css  js  c++  java
  • .net mvc 分页

    1、分页实体类

        public class PageDto
        {
            public int PageIndex { get; set; }
            public int PageSize { get; set; }
            public int PageCount { get; set; }
            public int TotalCount { get; set; }
        }

    后端代码

            public async Task<IActionResult> Index()
            {
                var page = HttpContext.Request.Query["page"].ToString();
                var pageSize = HttpContext.Request.Query["pageSize"].ToString();
    
                var searchDto = new FileSearchDto()
                {
                    PageIndex = string.IsNullOrWhiteSpace(page) ? 1 : Convert.ToInt32(page),
                    PageSize = string.IsNullOrWhiteSpace(pageSize) ? 5 : Convert.ToInt32(pageSize)
                };
    
                var results = await _service.GetPageList(searchDto);
                var model = new FileListViewModel() { Results = results };
                return View(model);
            }
    

      

    2、视图

    @using System.Text;
    @model BaseService.PageDto
    
    @if (Model.TotalCount > Model.PageSize)
    {
        var controller = Url.ActionContext.RouteData.Values["controller"];
        var action = Url.ActionContext.RouteData.Values["action"];
        var url = "/" + controller + "/" + action;
        var querys = Url.ActionContext.HttpContext.Request.Query;
        StringBuilder sb = new StringBuilder();
        sb.AppendFormat("/{0}/{1}?", controller, action);
        if (querys != null && querys.Count > 0)
        {
            foreach (var item in querys)
            {
                if (!item.Key.Equals("page"))
                {
                    sb.AppendFormat("{0}={1}", item.Key, item.Value);
                    sb.Append("&");
                }
            }
        }
        sb.Append("page");
    
        url = sb.ToString();
    
        <div style="position:fixed;bottom:-15px;text-align:center;84%">
            <ul class="pagination">
                <h5 class="pull-left">显示第 @((Model.PageIndex - 1) * Model.PageSize + 1) - @(Model.PageIndex * Model.PageSize > Model.TotalCount ? Model.TotalCount : Model.PageIndex * Model.PageSize) 条记录,共 @Model.TotalCount 条记录</h5>
                @if (Model.PageIndex > 1)
                {
                    <li><a href="@url=1" title="首页"><i class="fa fa-chevron-left"></i></a></li>
                }
                else
                {
                    <li class="disabled"><a href="javascript:;" title="首页"><i class="fa fa-chevron-left"></i></a></li>
    
                }
                @{
                    int pagesizeshow = 6;//每次显示分页按钮数量
                    int pagemin = Model.PageIndex - (int)(pagesizeshow / 2);
                    int pagemax = Model.PageIndex + (int)(pagesizeshow / 2);
                    if (Model.PageIndex + (int)(pagesizeshow / 2) >= Model.PageCount)
                    {
                        pagemin = Model.PageCount - (int)(pagesizeshow / 2);
                        pagemax = Model.PageCount;
                    }
                    if (Model.PageIndex <= (int)(pagesizeshow / 2))
                    {
                        pagemin = 1;
                        pagemax = pagesizeshow;
                    }
                    if (pagemin <= 0) { pagemin = 1; }
                    if (pagemax >= Model.PageCount) { pagemax = Model.PageCount; }
                    if (pagemin + pagesizeshow >= Model.PageCount) { pagemin = (Model.PageCount - pagesizeshow) >= 1 ? (Model.PageCount - pagesizeshow) : pagemin; }
    
                    if (pagemin == pagemax)
                    {
                        <li><a href="@url=@pagemin">@pagemin</a></li>
                    }
                    else
                    {
                        for (int i = pagemin; i <= pagemax; i++)
                        {
                            <li @if (Model.PageIndex == i) { <text> class="active" </text> }><a href="@url=@i">@i</a></li>
                        }
                    }
                }
    
                @if (Model.PageIndex < Model.PageCount)
                {
                    <li><a href="@url=@Model.PageCount" title="尾页"><i class="fa fa-chevron-right"></i></a></li>
                }
                else
                {
                    <li class="disabled"><a href="javascript:;" title="尾页"><i class="fa fa-chevron-right"></i></a></li>
                }
            </ul>
        </div>
    }
    

    3、调用

       @Html.Partial("~/Views/Pager/Create.cshtml", Model.Results.Page)

  • 相关阅读:
    大型门户网站架构设计的可伸缩性(转载)
    geodatabase的类型(翻译)
    在Windows Server 2008上部署SVN代码管理总结
    可以有效改进项目管理技能的十个过程(转载)
    GTD和知识管理
    查询Oracle版本号
    微软产品组里的十一类人(转载)
    在Win7中创建、部署WebService时遇到的访问被拒绝错误解决方法
    小议地理编码(转载)
    在Win7中将我的电脑快捷方式放入任务栏
  • 原文地址:https://www.cnblogs.com/lizhenhong/p/9151815.html
Copyright © 2011-2022 走看看