(C# ASP.NET Core) How to handle a FORM submit event

A form submit is triggerred by a submit button inside FORM tags. If the method is POST, which is most often the case, then the recommended handler is OnPostAsync. The method should be asynchronous because FORM data is invariably processed through database INSERT and UPDATE queries, which must be done asynchronously. The submitted data can be extracted through the two-way BindProperty. See the linked video for a better understanding.
(Rev. 18-Jun-2024)

Categories | About |     |  

Parveen,

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.