Can someone explain the following unexpected behaviour. I've tested this in both core 2.2 and 3.1.
I'm trying to make sense of posting 2 different forms on the same razor page.
Here's the HTML:
<form method="post">
<button type="submit">post</button>
</form>
<form method="post">
<button type="submit" asp-page-handler="WTF" >post wtf</button>
</form>
Here's the razor page behind:
public void OnGet()
{
Debug.WriteLine("Get");
}
public void OnPost()
{
Debug.WriteLine("Post");
}
public void OnPostWTF()
{
Debug.WriteLine("PostWTF");
}
I expect that when I press the 'post' button, the OnPost action gets called. When I press the 'post wtf' button, the OnPostWTF action gets called. That kind of happens. If I press the 'post' button initially, the expected action is called. But, as soon as I press the 'post wtf' button, ALL subsequent posts only call the OnPostWTF action, regardless which button is pressed!
Very nice guide here: https://www.learnrazorpages.com/razor-pages/handler-methods
The name of the handler is added to the form's action as a query string parameter.
So when you are posting without a handler then the current url is being posted to.
In your case it will be the one you have switched to with the query parameter handler=WTF
.
The best practice when you have multiple forms in the same page, is to setup a handler for each request to avoid such troubles.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.