在一个ASP.Net MVC 2应用程序中,我想做以下工作:在处理表单post的操作中,我想:
可以轻松地在form元素中设置target="_blank"
属性并添加以下jQuery脚本:
$(function () {
$("form").submit(function () {
window.location = "...";
});
});
操作处理程序返回的视图将在表单发布到的新窗口中呈现。
但是,让我们让它变得更棘手:
如果在执行操作时没有服务层错误,则执行上述操作。如果执行操作时有任何服务层错误,则不要打开新窗口,操作返回的视图必须显示在表单所在的同一窗口中。。
例如:假设服务层生成一个pdfDocument来显示给用户,如果一切正常,那么这个pdf必须显示在一个新的窗口中。
[HttpPost]
public ActionResult SomeAction(FormCollection form)
{
var serviceMethodParams = ... // convertion from the form data somehow
MemoryStream pdfDocument = null;
if (!serviceLayer.DoSomething(serviceMethodParams, out pdfDocument))
{
// Something went wrong, do not redirect, do not open new window
// Return the same view where error should be displayed
return View(...);
}
// The service method run ok, this must be shown in a new window and the origal window must be redirected somewhere else
return File(pdfDocument.ToArray(), "application/pdf");
}
注意,当服务返回true
时,原始解决方案工作得很好,但是如果服务返回false
,则显示错误的视图将显示在一个新窗口中,而原始窗口将被重定向到其他地方。
发布于 2011-03-23 13:12:09
在这种情况下,更好的选择是返回特定于该文档的URL,供用户指向,如果ajax调用成功,将从操作方法返回URL,然后打开指向该URL的新窗口。如果出现错误,您可以显示错误或类似的错误--基本上,数据是折叠在Json返回值中的。
在ajax调用之外,对我来说最可接受的模式是让视图重新呈现,然后附加一些启动javascript来打开指向特定URL的新窗口。
https://stackoverflow.com/questions/5405682
复制相似问题