【问题标题】:Input parameter of action is null when using asp-route-{value}使用asp-route-{value}时action的输入参数为null
【发布时间】:2022-01-26 07:42:21
【问题描述】:

我想下载一个名称已保存在我的数据库中的文件。我使用以下代码下载文件:

public async Task<IActionResult> DownloadPm(string fileName)
{
.
.
.
}

并在视图中使用以下代码:

<form method="get">
     <button type="submit" class="btn btn-success btn-table" asp-controller="Page" asp- 
     action="DownloadPm" asp-route-fileName="@item.mainFileName">ِDownload</button>
</form>

问题是DownloadPm动作中的输入参数是null

【问题讨论】:

    标签: asp.net-mvc


    【解决方案1】:

    您正在使用带有按钮的标签助手,该按钮实际上什么都不做,只是触发 html 表单。为了保留您当前的代码,您可以在表单中设置一个隐藏字段,该字段会将值发布到操作中。示例代码:

    <form method="get">
        <input name="fileName" type="hidden" value="@item.mainFileName">
        <button type="submit" class="btn btn-success btn-table" asp-controller="Page" 
        asp-action="DownloadPm">ِDownload</button>
    </form>
    

    更好的方法: 既然您使用的是 Http GET 并且表单中没有其他控件,那么为什么还要使用表单呢?而是使用锚元素并将其设置为按钮。像下面这样:

    <a class="btn btn-success btn-table" href="/Page/DownloadPm?fileName="@item.mainFileName">Download</a>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-26
      • 1970-01-01
      • 1970-01-01
      • 2012-05-13
      • 1970-01-01
      • 2020-11-16
      • 2023-03-22
      • 1970-01-01
      相关资源
      最近更新 更多