码迷,mamicode.com
首页 > Web开发 > 详细

.net mvc 分页

时间:2018-06-07 19:36:47      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:视图   sys   null   ++   page   syn   listview   实体   http   

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;width: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)

.net mvc 分页

标签:视图   sys   null   ++   page   syn   listview   实体   http   

原文地址:https://www.cnblogs.com/lizhenhong/p/9151815.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!