Home > Software engineering >  Razor page custom post method not firing
Razor page custom post method not firing

Time:10-09

Why this:

<form method="post">
  <nav>
   <ul class="pagination justify-content-center">
    <li class="page-item">
      <a class="page-link" asp-page-handler="filter" asp-route-id="1">1</a>
    </li>
   </ul>
  </nav>
 </form>

Does not fire OnPostFilter method inside backend?

public void OnGet()
    {
        ProdGoal = 15000;
        FillChart();
    }

    public void OnPostFilter(string id)
    {
        int MonthNum;
        if (int.TryParse(id, out MonthNum) && MonthNum >= 1 && MonthNum <= 12)
            FillChart(MonthNum);
        else
            FillChart();
    }

It always fires OnGet(). But when I would move the tag in front of it would work. Why is that? How should I make fire Post method.

CodePudding user response:

It is because the request method is get when you click <a class="page-link" asp-page-handler="filter" asp-route-id="1">1</a>,so you cannot go to post handler.

1.You can try to change the handler name to OnGetFilter:

public void OnGetFilter(string id)
    {
        int MonthNum;
        if (int.TryParse(id, out MonthNum) && MonthNum >= 1 && MonthNum <= 12)
            FillChart(MonthNum);
        else
            FillChart();
    }

2.If you don't want to change the handler name,try to change your html:

<form method="post" asp-page-handler="filter" asp-route-id="1">
    <nav>
        <ul class="pagination justify-content-center">
            <li class="page-item">
                <input type="submit" value="1"/>
            </li>
        </ul>
    </nav>
</form>

CodePudding user response:

I have changed

<input type="submit" value="1"/>

for

<button ... />

Now it works but I still don´t know why input is firing OnGet()

  • Related