How to have a a razor action link open in a new tab?

C#HtmlRazor

C# Problem Overview


I trying to get my link to open in a new tab (it must be in razor format):

    <a href="@Url.Action("RunReport", "Performance", new { reportView = Model.ReportView.ToString() }, new { target = "_blank" })" type="submit" id="runReport" class="button Secondary">@Reports.RunReport</a>

This is not working though. Anyone know how to do this?

C# Solutions


Solution 1 - C#

Just use the HtmlHelper ActionLink and set the RouteValues and HtmlAttributes accordingly.

@Html.ActionLink(Reports.RunReport, "RunReport", new { controller = "Performance", reportView = Model.ReportView.ToString() }, new { target = "_blank" })

Solution 2 - C#

Looks like you are confusing Html.ActionLink() for Url.Action(). Url.Action has no parameters to set the Target, because it only returns a URL.

Based on your current code, the anchor should probably look like:

<a href="@Url.Action("RunReport", "Performance", new { reportView = Model.ReportView.ToString() })" 
   type="submit" 
   id="runReport" 
   target="_blank"
   class="button Secondary">
     @Reports.RunReport
</a>

Solution 3 - C#

That won't compile since UrlHelper.Action(string,string,object,object) doesn't exist.

UrlHelper.Action will only generate Urls based on the action you provide, not <a> markup. If you want to add an HtmlAttribute (like target="_blank", to open link in new tab) you can either:

  • Add the target attribute to the <a> element by yourself:

      <a href="@Url.Action("RunReport", "Performance",
          new { reportView = Model.ReportView.ToString() })",
          target = "_blank" type="submit" id="runReport" class="button Secondary">
          @Reports.RunReport
      </a>
    
  • Use Html.ActionLink to generate an <a> markup element:

      @Html.ActionLink("Report View", "RunReport", null, new { target = "_blank" })
    

Solution 4 - C#

If your goal is to use the ActionLink helper and open a new tab:

@Html.ActionLink("New tab please", "Home", null , new { target = "_blank" })

@Html.ActionLink("New tab please", "Home", Nothing, New With {Key .target = "_blank"})

Solution 5 - C#

@Html.ActionLink(
"Pay Now",
"Add",
"Payment",
new { @id = 1 },htmlAttributes:new { @class="btn btn-success",@target= "_blank" } )

Solution 6 - C#

With Named arguments:

@Html.ActionLink(linkText: "TestTab", actionName: "TestAction", controllerName: "TestController", routeValues: null, htmlAttributes: new { target = "_blank"})

Solution 7 - C#

For

> @Url.Action

<a href="@Url.Action("Action", "Controller")" target="_blank">Link Text</a>

Solution 8 - C#

asp.net mvc ActionLink new tab with angular parameter

<a  target="_blank" class="btn" data-ng-href="@Url.Action("RunReport", "Performance")?hotelCode={{hotel.code}}">Select Room</a>

Solution 9 - C#

You are setting it't type as submit. That means that browser should post your <form> data to the server.

In fact a tag has no type attribute according to w3schools.

So remote type attribute and it should work for you.

Solution 10 - C#

<a href="@Url.Action("RunReport", "Performance", new { reportView = Model.ReportView.ToString() })" type="submit" id="runReport" target="_blank" class="button Secondary"> @Reports.RunReport </a>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionFairyQueenView Question on Stackoverflow
Solution 1 - C#GabeView Answer on Stackoverflow
Solution 2 - C#Erik PhilipsView Answer on Stackoverflow
Solution 3 - C#WDRustView Answer on Stackoverflow
Solution 4 - C#JoshYates1980View Answer on Stackoverflow
Solution 5 - C#Abiuth ArunView Answer on Stackoverflow
Solution 6 - C#usefulBeeView Answer on Stackoverflow
Solution 7 - C#Thisara SubathView Answer on Stackoverflow
Solution 8 - C#sanjeewaView Answer on Stackoverflow
Solution 9 - C#SlyView Answer on Stackoverflow
Solution 10 - C#FaisalView Answer on Stackoverflow