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

huangapple go评论54阅读模式
英文:

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

问题

The issue might be related to how the "page" parameter is being passed to your "OnGet" method. You can check the following:

  1. Ensure that the anchor tag (a tag) in your HTML is correctly passing the "page" parameter when a tag is clicked. Make sure the "asp-route-page" attribute is set correctly.

  2. Verify that JavaScript or any client-side code is not interfering with the value of the "page" parameter when the tag is clicked.

  3. Check the routing configuration in your ASP.NET application to ensure that the "page" parameter is correctly mapped to the "OnGet" method.

  4. Ensure that there are no default values or overrides for the "page" parameter elsewhere in your code that might be causing it to always be 1.

Double-checking these aspects should help you identify the cause of the issue where the "page" value is always 1.

英文:

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...

答案1

得分: 1

不会,因为OnGet将在提交表单后触发,我认为

@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>

另一种选择是保留相同的代码并读取查询字符串。

@Request.QueryString["page"]
英文:

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

@functions
{
    string Method1(int page = 1)
    {
       
    }
}

 &lt;div class=&quot;container&quot;&gt;
    @if (pager.TotalPages &gt; 0)
    {
        &lt;ul class=&quot;pagination justify-content-end&quot;&gt;
            @for (var pge = pager.StartPage; pge &lt;= pager.EndPage; pge++)
            {
                &lt;li class=&quot;page-item @(pge == pager.CurrentPage ? &quot;active&quot;: &quot;&quot;)&quot;&gt;
                    &lt;a class=&quot;page-link&quot;  onclick=Method1(@pge) &gt; @pge &lt;/a&gt;
                &lt;/li&gt;
            }
        &lt;/ul&gt;
    }
&lt;/div&gt;

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

@Request.QueryString[&quot;page&quot;]

huangapple
  • 本文由 发表于 2023年5月14日 02:31:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76244322.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定