【问题标题】:Why is asp-route-id not working in my form post?为什么 asp-route-id 在我的表单帖子中不起作用?
【发布时间】:2017-08-03 21:09:20
【问题描述】:

我有 asp-route-id 在一个实例中工作:

它使用这样的锚标记工作:

<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>

并像这样收到:

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

此方法接收正确的 id。

下一个示例在表单标签中具有如下所示的路由 ID:

<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>

我正在尝试在这里接收 ID:

    [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);
    }

但在这种情况下,参数为空。

它应该从 from 标签开始工作。我从 Adam Freemans APress MVC Core 书中的第一个 Identity Chapter 中得到了这个,当我为那个例子输入它时它就起作用了。

【问题讨论】:

  • 您是否检查了 HTML 中生成的表单?操作 URL 错误或模型绑定不起作用。通常,该参数将被称为 id 而不是 Id
  • 很好的命名约定。谢谢。不过仍在尝试修复。

标签: asp.net-mvc asp.net-core tag-helpers


【解决方案1】:

您必须将asp-route-id 更改为asp-route-Id,因为在“asp-router-”之后您必须将要发送到方法的参数的名称。

【讨论】:

    【解决方案2】:

    我检查了表单标签以响应 junnas 的 cmets。

    看起来像这样:

    <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>
    

    除了表单操作之外,请注意按钮上的额外 formaction="/Account/Delete"> 。

    我从锚点转换过来的按钮上留下了一个额外的 asp-route-id。

    我不得不把它从这里的按钮上取下来:

    <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>
    

    并制作这样的按钮:

    是 - 删除此用户

    【讨论】:

      猜你喜欢
      • 2021-08-06
      • 1970-01-01
      • 2018-05-04
      • 1970-01-01
      • 2021-12-31
      • 2014-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多