asp.net-mvc 如何从控制器关闭 ASP.NET MVC 页面?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/853738/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you close an ASP.NET MVC page from the controller?
提问by gfrizzle
I have an ASP.NET MVC app that opens a "Request" view in a new browser window. When the user submits the form, I'd like the window to close. What should my RequestController code look like to close the window after saving the request information? I'm not sure what the controller action should be returning.
我有一个 ASP.NET MVC 应用程序,它在新的浏览器窗口中打开一个“请求”视图。当用户提交表单时,我希望窗口关闭。保存请求信息后,我的 RequestController 代码应该如何关闭窗口?我不确定控制器操作应该返回什么。
回答by David
You could return a View that has the following javascript (or you could return a JavaScript result) but I prefer the former.
您可以返回一个包含以下 javascript 的视图(或者您可以返回一个 JavaScript 结果),但我更喜欢前者。
public ActionResult SubmitForm()
{
return View("Close");
}
View for Close:
查看关闭:
<body>
<script type="text/javascript">
window.close();
</script>
</body>
Here is a way to do it directly in your Controller but I advise against it
这是一种直接在您的控制器中执行此操作的方法,但我建议您不要这样做
public ActionResult SubmitForm()
{
return JavaScript("window.close();");
}
回答by Captain Kenpachi
Like such:
像这样:
[HttpPost]
public ActionResult MyController(Model model)
{
//do stuff
ViewBag.Processed = true;
return View();
}
The view:
风景:
<%if(null!=ViewBag.Processed && (bool)ViewBag.Processed == true){%>
<script>
window.close();
</script>
<%}%>
回答by womp
It sounds like you could return an almost empty View template that simply had some javascript in the header that just ran "window.close()".
听起来您可以返回一个几乎为空的视图模板,该模板仅在标题中包含一些刚刚运行“window.close()”的 javascript。
回答by Carlos Toledo
This worked for me:
这对我有用:
[HttpGet]
public ActionResult Done()
{
return Content(@"<body>
<script type='text/javascript'>
window.close();
</script>
</body> ");
}
回答by Greg Gum
This worked for me to close the window.
这对我关闭窗口有用。
Controller:
控制器:
return PartialView("_LoginSuccessPartial");
View:
看法:
<script>
var loginwindow = $("#loginWindow").data("kendoWindow");
loginwindow.close();
</script>
回答by Hemant
Using this you can close the window like this:
使用它,您可以像这样关闭窗口:
return Content("<script language='javascript'>window.close();</script>");

