ASP.NET Core Razor page form submit is generating AntiforgeryValidationException error

70 Views Asked by At

I am trying to check or uncheck a checkbox for individual records in a table and save the new check status back to the database. The form loads all the records correctly and shows the correct current checked or unchecked value for each record. On submitting the form with or without any changes an exception is thrown if there is more than 1 <input> in the table. I was able to make the form work correctly if the table only had the .isChecked field, but I need the Id field passed as well to update the record in the OnPost. Odd that everything works fine with just one <input>.

If the form table uses more than one <input>, the OnPostAsync() in Visual Studio throws this exception:

Exception thrown: 'Microsoft.AspNetCore.Antiforgery.AntiforgeryValidationException' in Microsoft.AspNetCore.Antiforgery.dll

Exception thrown: 'Microsoft.AspNetCore.Antiforgery.AntiforgeryValidationException' in System.Private.CoreLib.dll

I have tried @Html.AntiForgeryToken() in the form and also tried [ValidateAntiForgeryToken] for the OnPostAsync(). No luck.

.cshtml page

@if (Model.listAircraftModels != null && Model.listAircraftModels.Any())
{
    <form method="post">
    
        <div class="container">
        @* @Html.AntiForgeryToken() *@
            <table class="table">
                <thead>
                    <tr>
                        <td>Active</td>
                        <td>Mfg</td>
                        <td>Make</td>
                        <td>Model</td>
                        <td>Size Category</td> 
                    </tr>
                </thead>
                <tbody>
                    @for (int i = 0; i < Model.listAircraftModels.Count(); i++)
                    {
                        <tr>
                            <td>
                                 // If I comment out the hidden input everything works. More than one <input> in this or any <td> causes the error
                                <input asp-for="listAircraftModels[i].Id" type="hidden" />
                                <input asp-for="listAircraftModels[i].isChecked" type="checkbox" />
                            </td>
                            <td>
                                @Model.listAircraftModels[i].mfg
                            </td>
                            <td>
                                @Model.listAircraftModels[i].make
                            </td>
                            <td>
                                @Model.listAircraftModels[i].model
                            </td>
                            <td>
                                @Model.listAircraftModels[i].sizeCategory
                            </td> 
                        </tr>
                    }
                </tbody>
            </table>
            <input class="btn btn-dark me-2" type="submit"  value="Submit" />
        </div>
    </form>
}
else
{
    <div class="container">
        <p>No aircraft models found!</p>
    </div>
}

.cshtml.cs code

[Authorize()]
public class OverviewModel : PageModel
{
    private readonly IAircraftModelsRepository aircraftModelsRepository;

    [BindProperty]
    public List<AllAircraftModels> listAircraftModels { get; set; }

    public OverviewModel(IAircraftModelsRepository aircraftModelsRepository)
    {
        this.aircraftModelsRepository = aircraftModelsRepository;
    }

    public async Task OnGet()
    {
        listAircraftModels = (await aircraftModelsRepository.GetAsync())?.ToList();
    }

    //[ValidateAntiForgeryToken] Tried, but this but it didn't work 
    public async Task<IActionResult> OnPostAsync()
    {
        foreach (var aircraftModel in listAircraftModels)
        {
                await aircraftModelsRepository.UpdateAsync(aircraftModel);
        }
            return RedirectToPage("/AircraftModel/Overview");
    }
}

This is the model

public class AllAircraftModels
{
    public Guid Id { get; set; }
    public int modelId { get; set; }
    public string mfg { get; set; }
    public string make { get; set; }
    public string model { get; set; }
    public string sizeCategory { get; set; }
    public bool isChecked { get; set; }
}
0

There are 0 best solutions below