VS - Paginated

BootstrapPagination.cshtml

@model PaginationModel
<div class="pagination">
	<ul>
		@foreach (var link in Model.PaginationLinks)
  {
			@BuildLink(link)
		}
	</ul>
</div>

@helper BuildLink(PaginationLink link)
    {
    var liBuilder = new TagBuilder("li");
    if (link.IsCurrent)
    {
        liBuilder.MergeAttribute("class", "active");
    }
    if (!link.Active)
    {
        liBuilder.MergeAttribute("class", "disabled");
    }

    var aBuilder = new TagBuilder("a");
    if (link.PageIndex != null) {
        aBuilder.MergeAttribute("data-page", link.PageIndex.Value.ToString());
    }
    if (link.Url == null)
    {
        //aBuilder.MergeAttribute("href", "#");
    }
    else
    {
        aBuilder.MergeAttribute("href", link.Url);
        aBuilder.AddCssClass("paging");
    }
    if (link.DisplayText == "«")
    {
        aBuilder.SetInnerText("←");
    }
    else if (link.DisplayText == "»")
    {
        aBuilder.SetInnerText("→");
    }
    else
    {
        aBuilder.SetInnerText(link.DisplayText);
    }
    liBuilder.InnerHtml = aBuilder.ToString();
	
	@Html.Raw(liBuilder.ToString())
}

 

PaginatedList.cs

   public class PaginatedList<T> : List<T> {
        public int Offset { get; private set; }
        public int PageIndex { get; private set; }
        public int PageSize { get; private set; }
        public int TotalCount { get; private set; }
        public int TotalPages { get; private set; }

        /// <summary>
        /// For manually paing.
        /// </summary>
        /// <param name="source">The result of current page</param>
        /// <param name="totalCount"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        public PaginatedList(IEnumerable<T> source, int totalCount, int pageIndex, int pageSize)
        {
            Offset = pageIndex * pageSize;
            PageIndex = pageIndex;
            PageSize = pageSize;
            TotalCount = totalCount;
            TotalPages = (int)Math.Ceiling(TotalCount / (double)PageSize);

            this.AddRange(source);
        }

        public PaginatedList(IQueryable<T> source, int pageIndex, int pageSize) {
            Offset = pageIndex * pageSize;
            PageIndex = pageIndex;
            PageSize = pageSize;
            TotalCount = source.Count();
            TotalPages = (int)Math.Ceiling(TotalCount / (double)PageSize);

            this.AddRange(source.Skip(PageIndex * PageSize).Take(PageSize));
        }

        public PaginatedList(IQueryable<T> source, int pageSize, int? offset = null, int? pageIndex = null) {
            if (offset != null) {
                Offset = offset.Value;
                PageIndex = (int)Math.Ceiling((decimal)Offset / pageSize);
            } else if (pageIndex != null) {
                Offset = pageIndex.Value * pageSize;
                PageIndex = pageIndex.Value;
            } else {
                //offset = null and pageIndex = null
                throw new ArgumentNullException("offset and pageIndex");
            }
            PageSize = pageSize;
            TotalCount = source.Count();
            TotalPages = (int)Math.Ceiling(TotalCount / (double)PageSize);

            this.AddRange(source.Skip(Offset).Take(PageSize));
        }

        public bool HasPreviousPage {
            get {
                return (Offset > 0);
            }
        }

        public bool HasNextPage {
            get {
                return (Offset + PageSize < TotalCount);
            }
        }

    }

public PaginatedList<classname> classnameList { get; set; }

classnameList = new PaginatedList<EventCallDetail>(classList, count, (pageIndex > 0) ? pageIndex - 1 : 0, PageSize);

 @Html.Pager(PageSize, PageIndex + 1, classnameList.TotalCount).Options(o => o.DisplayTemplate("BootstrapPagination"))

posted on 2017-06-15 16:16  白马酒凉  阅读(139)  评论(0编辑  收藏  举报

导航