【问题标题】:Parse Id from view to controller将 Id 从视图解析到控制器
【发布时间】:2022-10-13 19:02:40
【问题描述】:

我遇到了一个奇怪的问题,我有一个填充的数据表,我试图将 StoreLicenseId 传递给我的控制器中的一个方法,但我的方法中的参数始终为空。 StoreLicenseId 在我的数据表中。一切看起来都正确,但我就是无法让它工作。

看法

<form method="post">
    <input class="btn btn-outline-info btn-1" type="submit" value="Terminal" asp-controller="Terminal" asp-action="TerminalInfo" asp-route-id="@item.StoreLicenseId">
</form>

控制器

public IActionResult TerminalInfo(string storeLicenseId)
{
    if (_context.StoreLicenses == null)
    {
        return NotFound();
    }

    var terminalModel = _context.StoreLicenses.FindAsync(storeLicenseId);
    if (terminalModel == null)
    {
        return NotFound();
    }
    return View(terminalModel);
}

【问题讨论】:

    标签: c# asp.net asp.net-mvc asp.net-core


    【解决方案1】:

    您应该将您的 asp-route 参数设置为与控制器操作预期的参数相同的名称,因此请更改此

     asp-route-id="@item.StoreLicenseId"
    

    asp-route-storeLicenseId="@item.StoreLicenseId"
    

    或将控制器操作中的名称更改为简单ID

    【讨论】:

      【解决方案2】:

      1.添加输入名称属性 2.删除路由ID

          <input class="btn btn-outline-info btn-1" type="submit" value="Terminal" asp-controller="Terminal" asp-action="TerminalInfo" name="storeLicenseId">
      

      或者: 使用 asp-for

      【讨论】:

        猜你喜欢
        • 2017-02-27
        • 1970-01-01
        • 2022-01-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-06-12
        • 2015-05-06
        • 1970-01-01
        相关资源
        最近更新 更多