0
votes

I created a brand new ASP .NET Core Razor web application and when I post with a custom asp-page-handler, OnPost() is hit, not OnPostContact(). This is such a simple form so there must be something simple I have overlooked.

Definitely a problem with my taghelpers, as I can get a model value to display like this:

@Model.FirstName

But not like this:

<input asp-for="FirstName" type="text" class="form-control" />

Here's the code, I can't see what's wrong here:

Index.cshmtl:

    <form method="post" asp-page-handler="Contact">
        @Html.AntiForgeryToken()
        <div asp-validation-summary="ModelOnly"></div>
        <span asp-validation-for="FirstName"></span>
        <input type="text" asp-for="FirstName" />
        <button type="submit">Send</button>
    </form>

Index.cshtml.cs:

public IActionResult OnPost()
{
    return Page();
}

public IActionResult OnPostContact()
{
    return Page();
}

_ViewImports.cshtml:

@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
1
Did you try to remove @Html.AntiForgeryToken() - Serge
Without @Html.AntiForgeryToken() I get a 400 error - Primico
In general you don't need @Html.AntiForgeryToken, it is added automatically. And I do put asp-page-handler="Contact" inside submit button tags and it works fine. - LazZiya

1 Answers

0
votes

I figured it was something stupid I was missing. There was a div tag further up in my page that had an extra quote and it somehow caused my tags not to work properly... an hour of time wasted.

<div class="container" data-aos="fade-up" ">