Copied to clipboard

Flag this post as spam?

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


  • mikkel 143 posts 365 karma points
    Feb 23, 2019 @ 16:23
    mikkel
    0

    Why is my paging skipping every second link

    hi every one My paging code is skipping every second link, like lin 1 works but link 2 dont and then link 3 work but link 4 dont and so on what have i done wrong i cant see it

    My code

    @{
    
        int pageSize = 1; // How many items per page
        int page; // The page we are viewing
    
        /* Set up parameters */
    
        if (!int.TryParse(Request.QueryString["page"], out page))
        {
            page = 1;
        }
    
        /* This is your basic query to select the nodes you want */
    
        var nodes = Model.Content.Children.Where(x => x.DocumentTypeAlias == "nyBegivenhed").OrderBy("CreateDate desc");
        int totalNodes = nodes.Count();
        int totalPages = (int)Math.Ceiling((double)totalNodes / (double)pageSize);
    
        /* Bounds checking */
    
        if (page > totalPages)
        {
            page = totalPages;
        }
        else if (page < 1)
        {
            page = 1;
        }
        <div class="container">
            <div class="row">
                <div class="col-lg-8 col-md-10 mx-auto">
                    <h1 class="main-page-heading">@Umbraco.Field("navnNyBegivenhed")</h1>
                    @foreach (var item in nodes.Skip((page - 1) * pageSize).Take(pageSize))
                    {
                        <div class="post-preview">
                            <a href="@item.Url">
                                <h2 class="post-title">@item.GetPropertyValue("navnNyBegivenhed")</h2>
                                @*<h2 class="post-title">@item.Name</h2>*@
    
                            </a>
                            <p class="post-meta">Skrevet af:&nbsp;@Umbraco.Field("creatorName")&nbsp;Den.&nbsp;@Umbraco.Field("createDate")</p>
                        </div>
                        <hr />
                    }
                    @{
                        if (totalPages > 1)
                        {
                            <div class="pagination-class">
                                <ul class="pagination">
                                    @if (page > 1)
                                    {
                                        <li><a style="margin-right: 10px;" class="btn btn-danger btn-pagination" href="?page=@(page-1)">Tilbage</a></li>
                                    }
                                    @for (int p = 1; p < totalPages + 1; p++)
                                    {
                                        <li style="margin-right: 10px;" class="@(p == page ? "active" : string.Empty) btn btn-danger btn-pagination">
                                            <a href="?page=@p">@p</a>
                                        </li>
                                    }
                                    @if (page < totalPages)
                                    {
                                        <li><a style="margin-right: 10px;" class="btn btn-danger btn-pagination" href="?page=@(page+1)">Næste</a></li>
                                    }
                                </ul>
                            </div>
                        }
                    }
                </div>
            </div>
        </div>
    
  • Chris Evans 137 posts 353 karma points c-trib
    Feb 24, 2019 @ 20:45
    Chris Evans
    0

    What do you mean when you say it doesn't work?

    e.g. is every second link in the pagination output missing? Or are they all showing but when you click on certain links it throws an error (in which case, is there an error message or screenshot you can post)?

    Or something else?

Please Sign in or register to post replies

Write your reply to:

Draft