0
可选打开一个新的窗口,在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
显示错误的视图显示在一个新窗口并被重定向别处原来的窗口最初的解决方案做工精细。