Search code examples
c#.netasp.net-mvcrazorrazor-pages

asp-route-... not passing the value to the OnGet method in razor


This is my code:

...
    <div class="container">
    @if (pager.TotalPages > 0)
    {
        <ul class="pagination justify-content-end">
            @for (var pge = pager.StartPage; pge <= pager.EndPage; pge++)
            {
                <li class="page-item @(pge == pager.CurrentPage ? "active": "")">
                    <a class="page-link" asp-route-page="@pge"> @pge </a>
                </li>
            }
        </ul>
    }
</div>

public async Task<IActionResult> OnGet(int page = 1)
        {
            TextPosts = (await _textPostRepository.GetAllAsync())
                .Where(x => !x.Tag.TagManagement.IsMeeting)
                .ToList();
            Tags = (await _tagRepository.GetAllAsync())
                .Where(x => !x.TagManagement.IsMeeting)
                .ToList();

            ViewData["Title"] = "Textes";

            InitializePage(page);

            return Page();
        }

Do you know why page value is always 1? The OnGet is triggered when the a tag is cliecked but the value is always 1. I'm wondering what is my mistake? Because clearly, page is well written and I do not know what to do more...


Solution

  • It will not , becuase OnGet will get fired on from submission, I think

    @functions
    {
        string Method1(int page = 1)
        {
           
        }
    }
    
     <div class="container">
        @if (pager.TotalPages > 0)
        {
            <ul class="pagination justify-content-end">
                @for (var pge = pager.StartPage; pge <= pager.EndPage; pge++)
                {
                    <li class="page-item @(pge == pager.CurrentPage ? "active": "")">
                        <a class="page-link"  onclick=Method1(@pge) > @pge </a>
                    </li>
                }
            </ul>
        }
    </div>
    

    Other option would be to keep the same code and read the query string.

    @Request.QueryString["page"]