Libraries > NopCommon > PagedList.cs

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
13 年 前
Hello,

I have a customized version of nopCommerce where on the customers.ascx control in the administration / modules folder I use the Common.PagedList<T>(...) function to get a new paged list of customers, which isn't standard, but that's why I came across this.

Your paged list is currently as follows:



    /// <summary>
    /// Paged list
    /// </summary>
    /// <typeparam name="T">T</typeparam>
    public class PagedList<T> : List<T>, IPagedList
    {
        /// <summary>
        /// Ctor
        /// </summary>
        /// <param name="source">source</param>
        /// <param name="pageIndex">Page index</param>
        /// <param name="pageSize">Page size</param>
        public PagedList(IQueryable<T> source, int pageIndex, int pageSize)
        {
            int total = source.Count();
            this.TotalCount = total;
            this.TotalPages = total / pageSize;

            if (total % pageSize > 0)
                TotalPages++;

            this.PageSize = pageSize;
            this.PageIndex = pageIndex;
            this.AddRange(source.Skip(pageIndex * pageSize).Take(pageSize).ToList());
        }

        /// <summary>
        /// Ctor
        /// </summary>
        /// <param name="source">source</param>
        /// <param name="pageIndex">Page index</param>
        /// <param name="pageSize">Page size</param>
        public PagedList(List<T> source, int pageIndex, int pageSize)
        {
            TotalCount = source.Count();
            TotalPages = TotalCount / PageSize;

            if (TotalCount % pageSize > 0)
                TotalPages++;

            this.PageSize = pageSize;
            this.PageIndex = pageIndex;
            this.AddRange(source.Skip(pageIndex * pageSize).Take(pageSize).ToList());
        }

        public int PageIndex { get; private set; }
        public int PageSize { get; private set; }
        public int TotalCount { get; private set; }
        public int TotalPages { get; private set; }

        public bool HasPreviousPage
        {
            get { return (PageIndex > 0); }
        }
        public bool HasNextPage
        {
            get { return (PageIndex + 1 < TotalPages); }
        }
    }



In the second function, the one that takes in a List of type T instead of the IQueryable of type T, the public properties are used before values are set for them.  On an unmodified 1.9 release, this is line 40-45.  Because these properties do not have values set for them, when you call this function, instead of having the page size set to the value passed in, it uses a default of zero, which causes the following exception to be thrown:

Attempted to divide by zero.

This exception is caught by the admin area properly, but the code for the second function should be changed (for lines 40-45) to match lines 20-25 from the IQueryable function that properly sets the properties before calling them...so basically the second function would look like:



        /// <summary>
        /// Ctor
        /// </summary>
        /// <param name="source">source</param>
        /// <param name="pageIndex">Page index</param>
        /// <param name="pageSize">Page size</param>
        public PagedList(List<T> source, int pageIndex, int pageSize)
    {
      int total = source.Count();
      this.TotalCount = total;
      this.TotalPages = total / pageSize;

      if (total % pageSize > 0)
        TotalPages++;

            this.PageSize = pageSize;
            this.PageIndex = pageIndex;
            this.AddRange(source.Skip(pageIndex * pageSize).Take(pageSize).ToList());
        }



after modifications.  I tested and confirmed that this does fix the problem.

Edit: Sorry, forgot to mention that this is for version 1.90 of nopCommerce.

Thanks
13 年 前
Thanks for info
12 年 前
Error in Medium Trust
boards/forum/1/new-products

Attempt by method 'DynamicClass.lambda_method(System.Runtime.CompilerServices.Closure)' to access type 'System.Linq.OrderedEnumerable`2<Nop.Core.Domain.Forums.ForumTopic,System.Int32>' failed.

Some light?
12 年 前
Thanks for reporting. It'll be fixed in the near time
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.