How can I have multiple submit buttons pass the same value from view to controller?

81 Views Asked by At

I have a form where you can delete a patient. There is also a second button which deletes a patient and also the user. These 2 buttons have different behaviours which I call in de "Delete" action and the "DeleteBoth" action. Since the view is loaded in action "Delete", the id in the url only gets passed onto the "Delete" action submit button. The "DeleteBoth" action gets an id with a null value, while the other does have an id value.

The button that deletes a patient only works, but the other button doesnt, since it doesnt get the id. Is there a way to make it so both buttons get the id from the url?

Heres the code in the view:

<form asp-action="">
    <input type="hidden" asp-for="patient.emailAddress" />
    <input type="submit" value="Delete patient only" class="btn btn-danger" asp-action="Delete"/>
    <input type="submit" value="Delete patient and user" class="btn btn-danger" asp-action="DeleteBoth"/>
    <a asp-action="Index">Back to List</a>
</form>

This is how the Delete method:

[HttpPost]
public IActionResult Delete(string id)

This is how the DeleteBoth method:

[HttpPost]
public IActionResult DeleteBoth(string id)

The URL when the user is clicking either button looks like this:

https://localhost:port/Patient/Delete/id
1

There are 1 best solutions below

0
SomeChickensWantToShank On

I found a solution! I added a name attribute to the hidden field containing the id:

<input type="hidden" asp-for="patient.emailAddress" name="emailAddress" />

Then i changed the actionresult ids to emailAddress instead of id and this worked for both the Delete and DeleteBoth method