dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.36k stars 9.99k forks source link

Blazor enhanced form handling doesn't honor button formaction attribute #51734

Closed DamianEdwards closed 11 months ago

DamianEdwards commented 11 months ago

When using Blazor enhanced form handling on a form that contains a <button type="submit"> element where that element also has a formaction attribute that modifies the URL the form should POST to, e.g. to add a querystring value, the value of the formaction attribute is ignored and the parent <form> element action is used instead. Removing Enhance from the form makes it behave the expected way.

@page "/posts"

<EditForm Model="this" FormName="posts" OnValidSubmit="HandleSubmit" Enhance>
    <table>
        <thead>
            <tr><th>Created</th><th>Title</th><th>Action</th></tr>
        </thead>
        <tbody>
        @if (posts.Count == 0)
        {
            <tr><td colspan="3"><button type="submit" formaction="/posts?action=create">Create Posts</button></td></tr>
        }
        else {
            foreach (var p in posts)
            {
                <tr>
                    <td>@p.Created</td>
                    <td>@p.Title</td>
                    <td>
                        <button type="submit" name="PostToDelete" value="@p.Id">delete</button>
                    </td>
                </tr>
            }
        }
        </tbody>
    </table>
</EditForm>

@code {
    [SupplyParameterFromForm]
    [Parameter]
    public int PostToDelete { get; set; }

    [SupplyParameterFromQuery]
    [Parameter]
    public string? Action { get; set; }
   ...
}

This was found when attempting to emulate what Razor Pages does to enable support for multiple form handlers.

AlexKovynev commented 11 months ago

@surayya-MS is any chance to backport to 8.0.x this and https://github.com/dotnet/aspnetcore/issues/51429 ? cause it both for 8.0.x

surayya-MS commented 11 months ago

@AlexKovynev, yes we are going to backport both of them.

AlexKovynev commented 11 months ago

@surayya-MS so maybe backport?:)