Home > Enterprise >  Input parameter of action is null when using asp-route-{value}
Input parameter of action is null when using asp-route-{value}

Time:12-28

I want to download a file that its name has been saved in my database. I use the following code to download the file:

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

And use the following code in view:

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

The problem is that the input parameter in DownloadPm action is null.

CodePudding user response:

You are using tag helper with a button which actually does nothing but triggers the html form. To keep your current code you can have a hidden field in the form which will post the value to the action. Sample code:

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

Better approach: Since you are using Http GET and there is no other control in the form then why bother with Form? Instead go with an anchor element and style it like a button. Like the followings:

<a  href="/Page/DownloadPm?fileName="@item.mainFileName">Download</a>
  • Related