Try our conversational search powered by Generative AI!

Post HTML form to specific method


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" />

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)

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?

Edited, Oct 16, 2023 14:08

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:


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

Great, that works! Thanks for the help.

Edited, Oct 17, 2023 11:56
* 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.