0

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>
maxshuty
  • 9,708
  • 13
  • 64
  • 77
Lukas Gund
  • 639
  • 1
  • 9
  • 27
  • https://stackoverflow.com/a/555970/4826740 It's not valid to do what you're trying to do. You should look into restructuring the code – maxshuty Nov 28 '19 at 14:16
  • 1
    Ahh man looks like my brain has.... I will try that later https://stackoverflow.com/questions/442704/how-do-you-handle-multiple-submit-buttons-in-asp-net-mvc-framework. Looks like a solution – Lukas Gund Nov 28 '19 at 14:22
  • You might want to look into AJAX posts too, it'll be a way easier in my opinion – maxshuty Nov 28 '19 at 14:29

1 Answers1

1

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>
 }
Ryan
  • 19,118
  • 10
  • 37
  • 53