我想下載一個檔案,其名稱已保存在我的資料庫中。我使用以下代碼下載檔案:
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。
uj5u.com熱心網友回復:
您正在使用帶有按鈕的標簽助手,該按鈕實際上除了觸發 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>
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/395546.html
標籤:asp.net-mvc
上一篇:獲取,控制器FromBody錯誤
