Why is asp-route-id not working in my form post?

2019-07-25 01:12发布

I have asp-route-id working in one instance:

It works from an anchor tag like this:

<a id="editModal" data-toggle="modal" data-target="#modal_box"
    class="btn btn-sm btn-primary"
    asp-action="Edit" asp-route-id="@user.Id">Edit</a>

and is received like this:

public async Task<PartialViewResult> DeleteConfirm(string Id) =>
        PartialView("_DeleteConfirm", await _userManager.FindByIdAsync(Id));

This method receives the correct id.

The next example has the route id in the form tag like this:

<div class="modal-footer">
    <form asp-controller="Account" asp-action="Delete" asp-route-id="@Model.Id" method="post">
        <button type="submit" class="btn btn-primary"
           asp-action="Delete">Yes - Delete this User</button>
        <a asp-action="Index" class="btn btn-default">Cancel</a>
    </form>
</div>

and I am trying to receive the Id here:

    [HttpPost]
    public async Task<IActionResult> Delete(string Id)
    {
        AppUser user = await _userManager.FindByIdAsync(Id);
        if (user != null)
        {
            IdentityResult result = await _userManager.DeleteAsync(user);
            if (result.Succeeded)
            {
                return RedirectToAction("Index");
            }
            else
            {
                AddErrorsFromResult(result);
            }
        }
        else
        {
            ModelState.AddModelError("", "User Not Found");
        }
        return View("Index", _userManager.Users);
    }

But in this case the parameter is coming in null.

It should work from the from tag. I got this from the first Identity Chapter in Adam Freemans APress MVC Core book and it worked when I typed it in for that example.

1条回答
甜甜的少女心
2楼-- · 2019-07-25 01:49

I inspected the form tag in response to junnas' comments.

It looked like this:

<form method="post" action="/Account/Delete/5066a97b-7eb4-4e1d-889b-3f3450adc1d6">
        <button type="submit" class="btn btn-primary" formaction="/Account/Delete">Yes - Delete this User</button>
        <a class="btn btn-default" href="/Account">Cancel</a>
    <input name="__RequestVerificationToken" type="hidden" value="CfDJ8At6yzynfgJHstUkF5jJTYgHHX0on9ajBa9NfD60YGbM7aRhKjDQUGkh4gPAMkwwOsnqUtDxSx87MnUvbxcefB7tGX1v0Xi38h_SoL_iakiFdJSpUbymp4tsr-cMFNK3kwYx7o42OumLkFbvFn1Nyun_8LDc0mmOvGN5LeeHiIq84cSJCaaiyR_2talw6Dpcvw" /></form>

Notice the extra formaction="/Account/Delete"> right on the button in addition to the form action.

I had an extra asp-route-id left over on a button I had changed over from an anchor.

I had to take it off the button here:

<form asp-controller="Account" asp-action="Delete" asp-route-id="@Model.Id" method="post">
        <button asp-route-id="@Model.Id" type="submit" class="btn btn-primary">Yes - Delete this User</button>
        <a asp-action="Index" class="btn btn-default">Cancel</a>
    </form>

and make the button like this:

Yes - Delete this User

查看更多
登录 后发表回答