伙伴关系。 Net MVC 2 申请 我想做以下工作:在处理一个职位时,我要:
- Redirect the user to other view in the current browser window
- Open a new window showing other info (other view)
That can be done easily setting the target="_blank"
attribute in the form element and adding the following jQuery script:
$(function () {
$("form").submit(function () {
window.location = "...";
});
});
行动手退还的观点将在新窗口上公布。
但是,让它成为一个小trick子:
- If there are no service layer errors when executing the action, then do the above.
- If there s any service layer error when executing the action, then do not open the new window and the view returned by the action must be shown in the same window where the form was in the first place.
例如:Imagine说,服务层产生一种pdf文件,向用户显示,如果所有东西都ok,并且必须在新的窗口中显示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");
}
Note that the original solution work fine when the service returns true
, but if the service returns false
the view showing the errors is shown in a new window and the original window is redirected somewhere else.