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.
+3
A:
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> 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();"); }
David Liddle
2009-05-12 16:50:13
Curious: why would you advise against the controller-only solution?
gfrizzle
2009-05-12 16:56:01
You're manipulating the UI by closing a window. Doesn't seem like a controller responsibility.
womp
2009-05-12 16:57:11
A:
It sounds like you could return an almost empty View template that simply had some javascript in the header that just ran "window.close()".
womp
2009-05-12 16:50:17