Razor Pages Post returns 400

Viewed 667

I have a ViewComponent on an Index page that makes a post to another page LogTransaction. When I post to the LogTransaction page, it returns a 400. I know the page is found because when I make a GET request to LogTransaction, it works perfectly. But any time I try to POST to it, it returns a 400.

Default.cshtml for ViewComponent contained on localhost:5001/Index:

@using BudgetTracker.BudgetSquirrel.Web.Pages

@model BudgetViewModel

...

<form action="/LogTransaction" method="post">
    <!-- Inputs Here -->
    <button type="submit">Save</button>
</form>

LogTransaction.cshtml.cs

namespace BudgetTracker.BudgetSquirrel.Web.Pages
{
    public class LogTransactionModel : BasePageModel // extends PageModel
    {
        public void OnGet()
        {}

        public IActionResult OnPost()
        {
            Console.WriteLine("Hi");
            return Page();
        }
    }
}

LogTransaction.cshtml

@page
@model BudgetTracker.BudgetSquirrel.Web.Pages.LogTransactionModel
@{
    ViewData["Title"] = "LogTransaction";
}

When I click the Save button, I get a 400 on the POST request at /LogTransaction. I know it's finding the page because I can make a successful GET request to /LogTransaction. It's just post requests that don't work.

1 Answers

I didn't have a CSRF token input in the form. You can either put it in manually or use the asp-page helper on the form instead of action. That helper automatically puts in the CSRF token input apperently

Related