Handle multiple forms in asp net core razor pages

kudvenkat · 39K views · 2020 · 10:17

Open on YouTube

Web developmenttutorial

Why Stack Overflow recommends it

Stack Overflow users link it when answering how to put multiple forms on one Razor Page and send each to a different OnPost handler.

Recommended by 0 people across 0 different months, 2020 to 2024.

What it is

A short tutorial by kudvenkat (Venkat) on handling more than one form on a single ASP.NET Core Razor Page. It shows how to use the asp-page-handler tag helper to route each form's POST to its own named handler method. It is part of his longer Razor Pages tutorial playlist.

What people said

Cites Farouk Belhocine · 2020-05 · Stack Overflow · on “How to implement two forms with separate BindProperties in Razor Pages?”

You can do that very simply based on the asp-page-handler attribute in your form. you can see example Here: [this video]

Cites SmartE · 2020-12 · Stack Overflow · on “Razor Pages POST Multiple Forms w/ Model Binding”

You can use different methods for different form POSTs by specifying the method's name as the value for the asp-page-handler tag helper on each form. More information can be found on this [this video] UPDATE For example, you have two POST methods in your Model class, namely: OnPostAdd and OnPostSubtract , the different forms to post to the different methods would look like this: <form method="post" asp-page-handler="Add"> <button type="submit">Add</button> </form> <form method="post" asp-page-handler="Subtract"> <button type="submit">Subtract</button> </form> PS: It doesn't matter whether your method is named OnPostAdd or OnPostAddAsync , ASP.NET Core still expects your handler name to be Add as

Cites PharaohS · 2024-02 · Stack Overflow · on “Why is my form directing to the OnPost method - multiple forms Razor Pages”

I have been following the below linked tutorial in setting up a Razor pages with multiple forms. [this video] In this tutorial it identifies that, of two forms (Form A and Form B), Form B's post can be directed to a post method other than 'OnPost', by using the asp-page-handler attribute and directing it to a different method other than OnPost (e.g. asp-page-handler="OnPostUpdateUnits"). I have implemented this in my code through the following tag helper: <form method="post" asp-page-handler="OnPostUpdateUnits"> <select asp-for="Units" asp-items="Model.UnitsSet"></select> <br /><button type="submit">Update Units</button> </form> Unfortunately using this form (i.e. clicking the Update Units button

Cites PharaohS · 2024-02 · Stack Overflow · on “Why is my form directing to the OnPost method - multiple forms Razor Pages”

It looks like removing the "OnPost" prefix fixed the issue. However, this is unexpected. [this video] (6:01) states that this is 'optional'. Including or not including it shouldn't make a difference.