J.Kirk.
J.Kirk.

Reputation: 973

Submit button linked to wrong form post

I have multiple post-forms in my view and my second form keeps hitting the first one.

My form looks like this:

<form asp-controller="Mentor" asp-action="DeleteAchievement"  asp-route-id="@item.Id" method="post">
    <button asp-route-id="@item.Id" type="submit" class="btn btn-primary">Delete</button>
 </form>

My controller method:

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> DeleteAchievement(string id)
{
  //delete
}

Even without routing the id it still doesn't hit it. It just hits the action of the view.

I inspected the generated html and the formaction is for unknown reasons not correspondent to the form but the submit button is linked to another form post.

<button type="submit" class="btn btn-primary" formaction="/mentor/achievements/634c650b-0659-4fc3-aea6-3ed2d597acb2">Slet</button>

I tried doing as this but with no luck. I also tried typing in the formaction for my button in my razor view but not a solution either. Has anyone encountered this problem?

Upvotes: 0

Views: 1043

Answers (1)

McKabue
McKabue

Reputation: 2222

  1. form action should be in the form not button
  2. form needs a hidden input field with item.Id as value

<form asp-controller="Mentor" asp-action="DeleteAchievement" method="post"> <input type="hidden" asp-for="item.Id" /> <button type="submit" class="btn btn-primary">Delete</button> </form>

Upvotes: 2

Related Questions