TempData always return null from view to controller

111 Views Asked by At

In my ASP.NET Core action method, at the end of the action, I am storing a value into TempData like this:

  TempData["FileURL"] = model.ToFileConvertedURL;

Here is my view

<form asp-controller="PdfToWordController" asp-action="Download">
    @if (TempData["FileURL"] != null)
    {
        <input asp-action="Download" type="submit" value="Download" class="btn btn-primary" />
    }
</form>

When I click the download button it should hit following action in controller.

[HttpPost]
[Route("pdf-to-word-converter/download")] 
public async Task<IActionResult> Download()
{
    string URL = string.Empty;

    if (TempData.ContainsKey("FileURL"))
        URL = TempData["FileURL"].ToString(); 

    byte[] fileBytes = null;

    // Download Word file
    if (!string.IsNullOrEmpty(URL))
    {
        fileBytes = System.IO.File.ReadAllBytes(URL);
    }

    return File(fileBytes, "application/force-download", "ddd");
}

But TempData always is null. I want to same action fire when user click download button again and again. What is the issue here? Is TempData is the right thing to use?

0

There are 0 best solutions below