可以选择在控制器操作中打开一个新窗口
在 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
,则显示错误的视图将显示在新窗口中,并且原始窗口将被重定向其他地方。
In a ASP.Net MVC 2 application I want to do the following: in the action that handles a form post I want to:
- 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 = "...";
});
});
The View returned by the action handler will be rendered in the new window where the form is posted to.
But, let's make it a little trickier:
- 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.
E.g.: Imagine that the service layer generates a pdfDocument to show to the user if everything is ok, and that pdf must be shown in a new window.
[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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在这种情况下,更好的选择是简单地返回特定于该文档的 URL 供用户指向,并且您的 ajax 调用在成功时会获取从操作方法返回的 URL,然后打开一个新窗口本身指向到该网址。出现错误时,您可以显示错误或类似的错误 - 基本上,该数据折叠在 Json 返回值中。
在 ajax 调用之外,对我来说最可接受的模式是重新渲染视图,然后附加一些启动 javascript 以打开指向该特定 URL 的新窗口。
In this situation, the better option would be to simply return a URL specific to that document for the user to point to, and your ajax call, when successful, takes the URL returned from your action method, and then opens a new window itself pointing to that URL. On error, you can display errors or some such - basically, that data is folded in a Json return value.
Outside of an ajax call, the most acceptable pattern to me is to have the view re render, then attach some startup javascript to open the new window pointing to that specific URL.