Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Sharmarke Hujale 103 posts 346 karma points
    Oct 27, 2015 @ 16:42
    Sharmarke Hujale
    0

    Paging problem!

    The other day when I implemented paging on my project, it worked just fine - but now it looks very weird, and I can't figure out what the problem is.

    enter image description here

    This is how it looks like

    And here is the code:

    Layout = "Master.cshtml";
    
    var pageSize = 3;
    var page = 1; int.TryParse(Request.QueryString["p"], out page);
    var items = Model.Content.Children().Where(x => x.IsDocumentType("BlogPost")).OrderByDescending(x => x.CreateDate);
    var totalPages = (int)Math.Ceiling((double)items.Count() / (double)pageSize);
    
    if (page > totalPages)
    {
        page = totalPages;
    }
    else if (page < 1)
    {
        page = 1;
    }
    
     @foreach (var blogPost in items.Skip((page - 1) * pageSize).Take(pageSize))
                {
                     <!--Article-->
                    <article class="post-preview">
                        <a href="@blogPost.Url">
                            <h2 class="post-title">@blogPost.Name</h2>
                            <h3 class="post-subtitle">@blogPost.GetPropertyValue("blogSubheader")</h3>
                        </a>
                        <p class="post-meta">
                            Posted by
                            <a href="@blogPost.Url">@Model.Content.CreatorName</a>
    
                            on @blogPost.CreateDate.ToString("MMMM d, yyyy")
                        </p>
                    </article>
                    <hr />
                }
                @if (totalPages > 1)
                {
                    <ul class="pagination">
                        @if (page > 1)
                        {
                            <li><a href="?page=@(page-1)">Prev</a></li>
                        }
                        @for (int p = 1; p < totalPages + 1; p++)
                        {
                            var active = (p == page) ? "active" : string.Empty;
                            <li class="@active">
                                <a href="?page=@p">1</a>
                            </li>
                        }
                        @if (page < items.Count())
                        {
                            <li><a href="?page=@(page+1)">Next</a></li>
                        }
                    </ul>
                }
    
  • Sharmarke Hujale 103 posts 346 karma points
    Oct 27, 2015 @ 20:27
    Sharmarke Hujale
    0

    Anyone?

  • Comment author was deleted

    Oct 27, 2015 @ 20:31

    Well from what I can see you are always outputting 1 in the link

    <li class="@active">
     <a href="?page=@p">1</a>
    </li>
    

    could you try doing this

    <li class="@active">
     <a href="?page=@p">@p</a>
    </li>
    
  • Nicholas Westby 2054 posts 7100 karma points c-trib
    Oct 27, 2015 @ 20:35
    Nicholas Westby
    0

    Are you talking about how every page says "1"? That's probably because you hardcoded a "1":

    Pager Loop

  • Sharmarke Hujale 103 posts 346 karma points
    Oct 27, 2015 @ 21:29
    Sharmarke Hujale
    0

    Thanks guys!

    It worked!

  • Sharmarke Hujale 103 posts 346 karma points
    Oct 27, 2015 @ 21:50
    Sharmarke Hujale
    0

    Now when I click "next" it says - blog?page=2 in the URL - but it doesn't go to the next page...

  • Sharmarke Hujale 103 posts 346 karma points
    Oct 29, 2015 @ 19:02
    Sharmarke Hujale
    0

    Someone who can help me out with this problem?

  • Nathan Woulfe 447 posts 1664 karma points MVP 5x hq c-trib
    Oct 29, 2015 @ 22:42
    Nathan Woulfe
    101

    Problem is here:

    var page = 1; int.TryParse(Request.QueryString["p"], out page);

    You're querying for 'p', when the parameter you set is 'page':

    <li><a href="?page=@(page+1)">Next</a></li>

    Update the first to change 'p' to 'page' and you should be fine.

  • Sharmarke Hujale 103 posts 346 karma points
    Oct 29, 2015 @ 22:49
    Sharmarke Hujale
    0

    You saved my day!

    Thanks!

  • Mohammad Javed 64 posts 373 karma points
    Nov 06, 2015 @ 09:35
    Mohammad Javed
    0

    Thank you - this helped me out as well to quickly implement paging.

Please Sign in or register to post replies

Write your reply to:

Draft