Search code examples
razor-pages.net-6.0

.net 6 what is the proper way to add a form to a _Layout and handle the post


I need to add a subscribe form to my Layout so it is on all pages in the right side area.

How do I handle the post action as layouts dont have a codefile?

What I have so far is my layout has a signup partial and in that partial is the form. At this point I ponder what to do and what is the correct and safe way on handling user input and the action handler.


Solution

  • Create a Razor page e.g. Subscribe.cshtml that handles the form submission and specify that page in the asp-page attribute of the form tag helper:

    <form method="post" asp-page="/Subscribe">
    

    Process the submission in the OnPost handler of SubscribeModel.