gfrizzle
gfrizzle

Reputation: 12609

How do you close an ASP.NET MVC page from the controller?

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.

Upvotes: 24

Views: 55542

Answers (7)

Glebka
Glebka

Reputation: 1668

You can close by this code:

return Content(@"<script>window.close();</script>", "text/html");

Upvotes: 2

Carlos Toledo
Carlos Toledo

Reputation: 2684

This worked for me:

[HttpGet]
public ActionResult Done()
{
    return Content(@"<body>
                       <script type='text/javascript'>
                         window.close();
                       </script>
                     </body> ");
}

Upvotes: 4

Greg Gum
Greg Gum

Reputation: 37909

This worked for me to close the window.

Controller:

 return PartialView("_LoginSuccessPartial");

View:

<script>
   var loginwindow = $("#loginWindow").data("kendoWindow");
   loginwindow.close();
</script>

Upvotes: 0

Hemant
Hemant

Reputation: 1

Using this you can close the window like this:

return Content("&lt;script language='javascript'>window.close();&lt;/script>");

Upvotes: -1

Captain Kenpachi
Captain Kenpachi

Reputation: 7215

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>
<%}%>

Upvotes: 3

David
David

Reputation: 15360

You could return a View that has the following javascript (or you could return a JavaScript result) but I prefer the former.

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();");
}

Upvotes: 37

womp
womp

Reputation: 116977

It sounds like you could return an almost empty View template that simply had some javascript in the header that just ran "window.close()".

Upvotes: 2

Related Questions