Let's say I have a form with multiple options on the home page. One of these is a partial view that takes a customerID. If the customerID is valid and has products, I return a CSV file like so:
public ActionResult CustomerProductsExport(string CustomerId)
{
var export = "\"ProductID\"\n";
IEnumerable<int> products = CustomerFactory.GetProducts(CustomerId);
export += string.Join("\n", products);
var aFileContent = Encoding.ASCII.GetBytes(export);
var aMemoryStream = new MemoryStream(aFileContent);
return File(aMemoryStream, "text/plain",
string.Format("{0}.csv", CustomerId));
}
There are, however, a couple cases where this will fail: either the customer ID doesn't exist, or they have no products. I would like to just return a javascript alert to indicate either of these cases. I've tried both FormMethod.Get and .Post with this:
return Javascript("alert('foo');");
But that always results in a literal string instead of running my javascript. How can I get my desired behavior or either delivering the file or giving a javascript alert without the post? I've also tried both a submit button vs an ActionLink... same results.