This is working :
@page
@model test0201.Pages.IndexModel
@{
}
<form method="post">
<button type="submit" class="btn btn-primary">Test</button>
</form>
using Microsoft.AspNetCore.Mvc.RazorPages;
namespace test0201.Pages
{
public class IndexModel : PageModel
{
public void OnGet()
{
}
public async Task OnPost()
{
Console.WriteLine("testtesttest");
}
}
}
And this is Not :
<form method="post">
<button type="submit" asp-page-handler="RefreshReturns" class="btn btn-primary">Test</button>
</form>
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
namespace test0201.Pages
{
public class IndexModel : PageModel
{
public void OnGet()
{
}
public async Task OnPostRefreshReturnsAsync()
{
Console.WriteLine("testtesttest");
}
}
}
Handler just not working. ChatGPT and Google didnt help. What i am missing here? First i thought my method not working, so i tried to test it and put just Console.Writeline.
Try to use F12 developer tool to check the generated Html elements, if the form looks as below, it means the Tag Helpers not available to the view:
In this scenario, you need to use the @addTagHelper
directive to make Tag Helpers available.
Since your application named test0201
, add the following directive
in the Views/_ViewImports.cshtml
file will make the TagHelpers be added to your project:
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@addTagHelper *, test0201
Or, you can directly add the following code in the Index view page (Index.cshtml):
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
After that the generated HTML like this:
Then, click the button, we can see that it will trigger the handler method (remember to set break point).
If still not working, try to check if there any error in the Console panel in F12 developer tool, and in the Network panel check the request: request url, http state code, response etc.