locked
Asp.Net MVC FileResult if not file exists RRS feed

  • Question

  • Hi Experts, 

    I have written an Action with return type as "FileResult" which takes Parameter as ID. For some ID the file may not exists. It downloads the file if exists. But if no file exists then I want to show a message on the screen that "File does not exists" and the control should stay on the same page. I am using Asp.Net MVC 5 framework

    How can I achieve the same ?

    • Moved by CoolDadTx Thursday, September 3, 2015 3:50 PM Wrong forums
    Thursday, September 3, 2015 10:58 AM

Answers

All replies

  • Please post ASP.NET related questions in the ASP.NET forums: http://forums.asp.net

    Their MVC forum is here: http://forums.asp.net/1146.aspx/1?MVC

    • Proposed as answer by Dave PatrickMVP Thursday, September 3, 2015 4:17 PM
    • Marked as answer by Just Karl Monday, September 14, 2015 4:50 PM
    Thursday, September 3, 2015 11:26 AM
  • This is not an Asp.NET forum, you should direct them to the Mike's suggested link but you can use a Viewbag for the simplest solution and show that value in your view. 

    public FilePathResult YourMethod(int id) {

    if(!database.Files.Any(x=>x.Id==id)

    {

    TempData["yourMessage"] = "File does not exist!";

    return RedirectToView("CurrentView"); // change the name with the view you want to be

    } }


    In your view add the tempData like this for example: 

    <div>@TempData["yourMessage"]  </div>

    Thursday, September 3, 2015 11:44 AM