Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 902
gfrizzle Avatar asked May 12 '09 16:05

gfrizzle


People also ask

How do you close a browser in C#?

window. close(); (in JavaScript), but depending on the browser and the situation that may be ignored. If it works, it'll close just the window/tab housing that code.

How do you fix Scripts may close only the windows that were opened by it?

Scripts may close only the windows that were opened by it. A workaround now is redirect user to another page rather than close the window, you could redirect user to a notification page to show "The items has been closed successfully" using window. location.


1 Answers

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();"); } 
like image 165
David Avatar answered Nov 22 '22 11:11

David