Home > Back-end >  Razor page form is not working with post request
Razor page form is not working with post request

Time:12-17

I'm learning .NET and I was trying to pass data from a simple form without tag-helpers, but the form doesn't work and I don't know why. It is very simple, a index file

@page
@model WebApplication1.Pages.IndexModel
@{
    Layout = null;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Home</title>
</head>
<body>
<form  method="post">
    
    <input type="text"  placeholder="Your First Name" name="FirstName">
    <input type="text"  placeholder="Your Last Name" name="LastName">
    <input type="submit" value="Submit">
</form>

Its model

namespace WebApplication1.Pages
{
    public class IndexModel : PageModel
    {
        [BindProperty]
        public InputModel Input { get; set; }

        public void OnGet()
       {
       }

       public IActionResult OnPost()
       {
           return RedirectToPage("Success");
       }

       public class InputModel
       {
           public string FirstName { get; set; }
        
           public string LastName{ get; set; }
      }  
   }
}

When it hit the post method it was suppose to redirect to the page Success.

CodePudding user response:

A 400 status code in Razor Pages is most often the result of a failed request verification check. The verification code is generated in a hidden field by the form tag helper when the method is set to post. If you have disabled tag helpers (or simply not enabled them), you can generate the token by including

@Html.AntiForgeryToken()

within the form. Alternatively, you can disable request verification by adding the IgnoreAntiforgeryToken to the PageModel class:

[IgnoreAntiforgeryToken(Order = 1001)]
public class IndexModel : PageModel 

CodePudding user response:

        <form method="post" >
                                    <div >
                                        <div ><span
                                                ></span></div>
                                        <input type="text"  asp-for="FirstName" placeholder="FirstName"
                                            required>
                                    </div>
                                    <div >
                                        <div ><span
                                                ></span></div>
                                        <input type="password"  asp-for="LastName"
                                            placeholder="LastName" required>
                                    </div>
                                    <div >
                                        <button type="submit" id="submitBtn"
                                            >Go!</button>
                                    </div>
                                </form>

    //Login.cshtml.cs
    
    [BindProperty]
    public string FirstName { get; set; }

    [BindProperty]
    public string LastName { get; set; }

public async Task<IActionResult> OnPostAsync()
        {
            LoginRequest request = new LoginRequest
            {
                FirstName = FirstName,
                LastName = LastName,
            };

            .
            .
            .
            
            if (..)
            {
                return RedirectToPage("./Home");
            }

            
            return Page();
        }
  • Related