Lukas Gund
Lukas Gund

Reputation: 711

How to set a Razor Html.BeginForm within another Html.BeginForm

Description

I want to have two buttons to edit and delete an identity role. The edit button works but if I want to delete a role the site is reloading but nothing happens.

The post function will not be called. Is there another way to have a post form in another post form?

Code

<div class="row mb-3">
    <div class="col-12">
        <div class="card">
            <div class="card-body">
                <h4 class="card-title">Rollennamen bearbeiten</h4>
                @using(Html.BeginForm("EditRole", "Administration", FormMethod.Post)) {
                    <div class="form-group row">
                        <label asp-for="EditRole.RoleName" class="col-sm-12 col-md-3"></label>
                        <div class="col-sm-12 col-md-9">
                            <input asp-for="EditRole.Id" type="hidden" value="@roles.Id" />
                            <input asp-for="EditRole.RoleName" class="form-control" value="@roles.Name" />
                            <span asp-validation-for="EditRole.RoleName" class="text-danger"></span>
                        </div>
                    </div>
                    <div class="d-flex flex-row-reverse">
                        <button type="submit" class="btn btn-success">Rolle bearbeiten</button>
                        @using(Html.BeginForm("DeleteRole", "Administration", FormMethod.Post)) {
                            <input name="id" type="hidden" value="@roles.Id" />
                            <button type="submit" class="btn btn-danger mr-2">Rolle löschen</button>
                        }
                    </div>
                }
            </div>
        </div>
    </div>
</div>

Upvotes: 0

Views: 135

Answers (1)

Ryan
Ryan

Reputation: 20106

You could handle multiple submit in one form use asp-action like

@using (Html.BeginForm(FormMethod.Post))
{      
    <div class="d-flex flex-row-reverse">
        <input type="submit" value="Save" class="btn btn-success" asp-controller="Administration" asp-action="EditRole" />
        <input type="submit" value="Delete" class="btn btn-danger" asp-controller="Administration" asp-action="DeleteRole" />

    </div>
 }

Upvotes: 1

Related Questions