【发布时间】:2011-03-23 13:07:14
【问题描述】:
在 ASP.Net MVC 2 应用程序中,我想要执行以下操作:在处理表单发布的操作中,我想要:
- 将用户重定向到当前浏览器窗口中的其他视图
- 打开一个显示其他信息的新窗口(其他视图)
这可以很容易地在表单元素中设置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,则显示错误的视图会显示在新窗口中,并且原始窗口会重定向到其他位置。
【问题讨论】: