Recap of the FORM learnt so far
Please see the previous tutorial for continuity - (C# ASP.NET Core) Creating an HTML FORM with Razor Pages
We have learnt how the various fields of a [BindProperty]
are connected to a form on a razor page. When the form is submitted, this same property contains the values filled and submitted by the user.
Video Explanation
Please watch the following youtube video:
Recommended Handler for a FORM POST
(see the linked video for details)
A form POST should be handled by an asynchronous OnPostAsync handler because a form POST is invariably processed through database updates and inserts, which must always be done asynchronously.
public class IndexModel : PageModel { [BindProperty] public User UserData { get; set; } // asynchronous handler recommended public async Task<IActionResult> OnPostAsync() { // "UserData" BindProperty (see above) // contains all the values entered by the user // process them here // . . . and redirect return RedirectToPage( any parameters . . . ); } // other members of the class . . . }
The [BindProperty]
contains the posted data as can be seen in the breakpoint.
This Blog Post/Article "(C# ASP.NET Core) How to handle a FORM submit event" by Parveen is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.