Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open mvc view in new window from controller

Is there any way to open a view from a controller action in a new window?

public ActionResult NewWindow() {     // some code     return View(); } 

How would I get the NewWindow.cshtml view to open in a new browser tab?

I know how to do it from a link in the view - that is not the question. Has anyone figured out a way to do it from the controller action?

like image 833
Joe Avatar asked Jan 24 '13 19:01

Joe


People also ask

How do I redirect a view from a controller?

You can use the RedirectToAction() method, then the action you redirect to can return a View. The easiest way to do this is: return RedirectToAction("Index", model); Then in your Index method, return the view you want.

How do I move from one view to another in MVC?

Call the appropriate /controller/action in your respective button click handlers. In your case for the register button handler direct it to /home/register. Have a view for your register functionality. In the register action of your home controller return the view you want to show.

How do you add a view to a controller?

Create view from controller In MVC 5Open the “HomeController” >> Set cursor inside the Action Method >> Right click on inside the action method >> click on [Add View] as follow. Provide the view name and select the appropriate layout and click on the “Add” button as follows.


1 Answers

This cannot be done from within the controller itself, but rather from your View. As I see it, you have two options:

  1. Decorate your link with the "_blank" attribute (examples using HTML helper and straight HMTL syntax)

    • @Html.ActionLink("linkText", "Action", new {controller="Controller"}, new {target="_blank"})
    • <a href="@Url.Action("Action", "Controller")" target="_blank">Link Text</a>
  2. Use Javascript to open a new window

    window.open("Link URL")

like image 109
Tommy Avatar answered Sep 18 '22 11:09

Tommy