/// <summary>
 /// fetch part of the queryable data and build pagination data for view to render pagination links
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="list"></param>
 /// <param name="page">current page number</param>
 /// <param name="pageSize">item per page</param>
 /// <param name="viewData">used for store pagination data, which for view to render pagination links</param>
 /// <returns></returns>
 public static IQueryable <T> Paginate <T>(this IQueryable <T> list, int page, int pageSize, ViewDataDictionary viewData)
 {
     viewData["_PaginationData"] = PaginationData.Build(
         new PaginationOption
     {
         TotalCount   = list.Count(),
         Page         = page,
         PageSize     = pageSize,
         NextText     = Strings.next,
         PreviousText = Strings.previous
     });
     return(list.Skip((page - 1) * pageSize).Take(pageSize));
 }
Exemplo n.º 2
0
        public static PaginationData Build(PaginationOption option)
        {
            PaginationData result = new PaginationData();

            result.TotalCount  = option.TotalCount;
            result.CurrentPage = option.Page;
            int page = option.Page;

            result.TotalPages   = (int)Math.Ceiling(option.TotalCount / (double)(option.PageSize ?? _pageSize));
            result.PreviousLink = new PaginationLink {
                Text = option.PreviousText ?? _previousText, Page = page - 1, IsLink = page != 1, HtmlClass = option.HtmlClass ?? _htmlClass
            };
            result.NextLink = new PaginationLink {
                Text = option.NextText ?? _nextText, Page = page + 1, IsLink = page != result.TotalPages, HtmlClass = option.HtmlClass ?? _htmlClass
            };
            return(result);
        }
Exemplo n.º 3
0
        /// <summary>
        /// render pagination links (assume that there is PaginationData stored in ViewData)
        /// </summary>
        /// <param name="helper"></param>
        /// <returns></returns>
        public static string Paginate(this HtmlHelper helper)
        {
            string actionName = helper.ViewContext.RouteData.Values["action"].ToString();

            PaginationData paginationData = helper.ViewData["_PaginationData"] as PaginationData;

            if (paginationData == null)
            {
                return("no pagination data");
            }
            StringBuilder output = new StringBuilder();

            output.Append(helper.NavigationTag(paginationData.PreviousLink, actionName));

            output.Append(" ");

            output.Append(helper.NavigationTag(paginationData.NextLink, actionName));

            return(output.ToString());
        }