Post HTML form to specific method

Vote:
 

I have a Razor Page template with an HTML form. I wish to trigger a specific method on the post event.

The form is as follows:

<form method="post" asp-page-handler="Edit">
    <input type="text" name="firstName" />
    <input type="submit" value="Submit" />
</form>

And the page model:
public class TestPageModel : RazorPageModel<TestPage>
{

...
    public void OnPostEdit(string firstName)
    {
        var s = firstName;
    }
}

According to ASP .NET Core documentation and handler naming conventions, asp-page-handler="Edit" should trigger the OnPostEdit method.

If I add the general OnPost method, it will be triggered on the form submit:

        public void OnPost(string firstName)
        {
            OnGet();
        }

But I have several forms on the page template, and each needs its own method.

I cannot find any Optimizely documentation regarding this, and I suspect the issue is due to Optimizely routing.

Has anybody solved this?

#310914
Edited, Oct 16, 2023 14:08
Ted
Vote:
 

Perhaps not by-the-book, by you can simply add ?handler=edit to the form action URL if you're posting to the current page:

Example:

@using (Html.BeginForm(FormMethod.Post, new { action = "?handler=edit" }))
{
    <button type="submit">Invoke OnPostEdit()</button>
}
#311021
Edited, Oct 17, 2023 11:54
Vote:
 

Great, that works! Thanks for the help.

#311022
Edited, Oct 17, 2023 11:56
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.