81

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?

FairyQueen
  • 2,283
  • 7
  • 37
  • 57

10 Answers10

137

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" })
Gabe
  • 49,577
  • 28
  • 142
  • 181
47

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>
Erik Philips
  • 53,428
  • 11
  • 128
  • 150
23

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" })
    
Tot Zam
  • 8,406
  • 10
  • 51
  • 76
WDRust
  • 3,663
  • 1
  • 19
  • 23
18

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"})
JoshYates1980
  • 3,476
  • 2
  • 36
  • 57
9
@Html.ActionLink(
"Pay Now",
"Add",
"Payment",
new { @id = 1 },htmlAttributes:new { @class="btn btn-success",@target= "_blank" } )
Bugs
  • 4,491
  • 9
  • 32
  • 41
Abiuth Arun
  • 169
  • 1
  • 4
  • 2
    While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value. – Alex Riabov Sep 17 '18 at 14:55
7

For

@Url.Action

<a href="@Url.Action("Action", "Controller")" target="_blank">Link Text</a>
Thisara Subath
  • 635
  • 6
  • 16
6

With Named arguments:

@Html.ActionLink(linkText: "TestTab", actionName: "TestAction", controllerName: "TestController", routeValues: null, htmlAttributes: new { target = "_blank"})
usefulBee
  • 9,250
  • 10
  • 51
  • 89
1

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>
sanjeewa
  • 564
  • 1
  • 5
  • 17
0

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.

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
Sly
  • 15,046
  • 12
  • 60
  • 89
0

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

Faisal
  • 190
  • 1
  • 11