【发布时间】:2011-09-07 23:58:32
【问题描述】:
假设我的主页上有一个包含多个选项的表单。其中之一是采用 customerID 的局部视图。如果 customerID 有效并且有产品,我会返回一个 CSV 文件,如下所示:
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));
}
但是,有几种情况会失败:客户 ID 不存在,或者他们没有产品。我只想返回一个 javascript 警报来指示这些情况中的任何一种。我已经尝试过 FormMethod.Get 和 .Post :
返回 Javascript("alert('foo');");
但这总是会产生一个文字字符串,而不是运行我的 javascript。如何在没有帖子的情况下获得我想要的行为或交付文件或发出 javascript 警报?我还尝试了提交按钮和 ActionLink... 相同的结果。
【问题讨论】:
标签: c# ajax asp.net-mvc