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);
}