Esempio n. 1
0
        public BlogPaginationViewModel(int pageNumber,
                                       int pageSize,
                                       int totalCount,
                                       bool showLastPage,
                                       int lengthOfPager       = 5,
                                       string queryStringParam = "page",
                                       string activeClass      = "active",
                                       string disabledClass    = "disabled",
                                       string nextLabel        = "Next",
                                       string prevLabel        = "Previous"
                                       )
        {
            Page             = pageNumber;
            PageSize         = pageSize;
            TotalCount       = totalCount;
            NumberOfPages    = (int)Math.Ceiling((double)TotalCount / PageSize);
            LengthOfPager    = lengthOfPager;
            QueryStringParam = queryStringParam;
            ActiveClass      = activeClass;
            DisabledClass    = disabledClass;
            NextLabel        = nextLabel;
            PreviousLabel    = prevLabel;
            ShowLastPage     = showLastPage;

            //set start and end range indexes
            double result = (double)Page / LengthOfPager;

            StartRangeIndex = (int)(Math.Floor(result) * LengthOfPager);

            if (Page % LengthOfPager == LengthOfPager)
            {
                StartRangeIndex = StartRangeIndex + LengthOfPager;
            }

            if (StartRangeIndex == 0)
            {
                StartRangeIndex = 1;
            }

            EndRangeIndex = StartRangeIndex + (LengthOfPager - 1);

            //set rel/prev for seo
            string relPrev = "";

            if (Page > 1)
            {
                relPrev = string.Format("<link rel=\"prev\" href=\"{0}\" />", BlogUtils.GetPagedUrl(QueryStringParam, Page - 1, true));
            }

            if (Page != NumberOfPages && NumberOfPages != 0)
            {
                if (relPrev.Length > 0)
                {
                    relPrev += Environment.NewLine;
                }
                relPrev += string.Format("<link rel=\"next\" href=\"{0}\" />", BlogUtils.GetPagedUrl(QueryStringParam, Page + 1, true));
            }

            AgilityContext.Page.MetaTagsRaw += relPrev;
        }