Copied to clipboard

Flag this post as spam?

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


  • Rayyan 46 posts 238 karma points
    Jul 16, 2017 @ 22:41
    Rayyan
    0

    Limit the number of pages displayed in search results page pagination

    Hello,

    I'm not a developer and I hope you guys help me with this.

    The number of pages displayed in search results page pagination using the below code equal the number of actual pages, and I need to limit that please.

    Thank you!

      foreach (var searchResult in searchResults.Skip((currentPage - 1) * pageSize).Take(pageSize))
                    {
                        @Html.Partial("ListPlaceItem", new ViewDataDictionary { { "placeId", searchResult.Id } })
                    }
    
                    <nav aria-label="Page navigation">
                        <ul class="pagination">
                            <li class="@(currentPage == 1 ? "disabled" : "")">
                                <a href="@(currentPage > 1 ? "/search-results?Query=" + term + "&page=" + (currentPage - 1).ToString() : "#")" aria-label="Previous">
                                    <span aria-hidden="true">&laquo;</span>
                                </a>
                            </li>
                            @for (var i = 1; i <= totalPages; i++)
                            {
                                <li>
                                    <a href="/search-results?Query=@term&page=@i">@i</a>
                                </li>
                            }
                            <li class="@(currentPage == totalPages ? "disabled" : "")">
                                <a href="@(currentPage < totalPages ? "/search-results?Query=" + term + "&page=" + (currentPage + 1).ToString() : "#")" aria-label="Next">
                                    <span aria-hidden="true">&raquo;</span>
                                </a>
                            </li>
                        </ul>
                    </nav>
                }
            }
    

    Pagination

    I have tried to do something similar to the following but I didn't succeed,

                                    @if (page > 1)
                                    {
                                        <li><a href="?page=@(page-1)">Previous</a></li>
                                    }
                                    @for (int p = 1; p <= totalPages; p++)
                                    {
                                        var linkClass = (p == page) ? "active" : "";
                                        if ((p >= page - 0 && p <= page + 5)
                                            || totalPages <= 7)
                                        {
                                            <li class="@Html.Raw(linkClass)">
                                                <a href="?page=@p">@p</a>
                                            </li>
                                        }
                                    }
                                    @if (page < totalPages)
                                    {
                                        <li>
                                            <a href="?page=@(page+1)">
                                                Next
                                            </a>
                                        </li>
                                    } 
    
  • Alex Skrypnyk 6132 posts 23951 karma points MVP 7x admin c-trib
    Jul 17, 2017 @ 09:17
    Alex Skrypnyk
    0

    Hi Rayyan

    Why isn't this code working?

    What is the error?

    Thanks,

    Alex

  • Rayyan 46 posts 238 karma points
    Jul 17, 2017 @ 09:22
    Rayyan
    0

    Hi, the second code is working well on another page that I have, I have tried to do the same thing with the wanted one but it didn't work. What do you think?

  • Alex Skrypnyk 6132 posts 23951 karma points MVP 7x admin c-trib
    Jul 17, 2017 @ 10:47
    Alex Skrypnyk
    0

    Can you check what is value of "totalPages"?

    Alex

  • Rayyan 46 posts 238 karma points
    Jul 17, 2017 @ 14:23
    Rayyan
    0

    I guess not, how to do that?

  • Alex Skrypnyk 6132 posts 23951 karma points MVP 7x admin c-trib
    Jul 17, 2017 @ 14:31
    Alex Skrypnyk
    0

    Add this to your code:

    @totalPages
    

    And check the page, it should be rendered.

  • Rayyan 46 posts 238 karma points
    Jul 17, 2017 @ 17:37
    Rayyan
    0

    I don't understand, what the changes that can make? The first code I have displays the whole pages

  • Alex Skrypnyk 6132 posts 23951 karma points MVP 7x admin c-trib
    Oct 13, 2017 @ 10:04
    Alex Skrypnyk
    0

    Hi Rayyan

    Did you fix this issue?

    Thanks.

    Alex

Please Sign in or register to post replies

Write your reply to:

Draft