Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to return File or ErrorMessage from Asp.net-mvc controller action?

I have the following JavaScript code and controller action in my ASP.NET-MVC project:

Javascript:

$("#exportPPT").live('click', function (e) {
    window.location.href = "/Initiative/GenerateFile" + GenerateParams();
});

C# Controller:

    public ActionResult GenerateFile(MyParams myParams)
    {
        var template = Server.MapPath(PPT_ROOT + "/template.pptx");
        IEnumerable<Order> orders = Model.GetOrders(myparams);
        var pptResults = GeneratePowerpointFile(orders);
        return File(pptResults.Content, "application/vnd.ms-powerpoint", pptResults.FileName);
    }

But under certain conditions, let's say when orders.Count() is 0 then instead of generating a file, I would rather have an error message back to the user saying that you have an error.

What is the best way to achieve this given the code above? I thought of changing it to an AJAX call but I wasn't sure how to download my Fie() and package that inside a JSON request (or if that was supported).

like image 866
leora Avatar asked Jan 03 '14 02:01

leora


1 Answers

I would initiate a $.get request to another controller action that would check for the order count. Return that value, along with an error message if appropriate. Display the error message when it's needed, otherwise process your redirect to download your file. It's an extra call to the controller but it allows you to have full control and handle the possibility of an error without redirecting your user.

$("#exportPPT").live('click', function (e) {
  $.get( "/Initiative/CheckForOrders" + GenerateParams(), function( data ) {
    if (data.IsValid) {
      window.location.href = "/Initiative/GenerateFile" + GenerateParams();
    } else {
      alert(data.ErrorMessage); // or show a div containing error message
    }
  });
});

Controller Action:

public ActionResult CheckForOrders(MyParams myParams)
{
  IEnumerable<Order> orders = Model.GetOrders(myparams);
  if (orders.Any())
    return Json(new { IsValid=true }, JsonRequestBehavior.AllowGet);

  return Json(new { IsValid=false, ErrorMessage="No orders" }, JsonRequestBehavior.AllowGet);
}
like image 194
Anthony Shaw Avatar answered Oct 23 '22 12:10

Anthony Shaw