如何在新标签页中打开剃刀动作链接?


69

我试图让我的链接在新标签页中打开(它必须为剃须刀格式):

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

虽然这不起作用。有人知道怎么做吗?


6
您为什么要尝试将它放入其中Url.Action呢?只需放入<a>标签本身即可。
乔尔·埃瑟顿

Answers:


43

似乎您在将Html.ActionLink()Url.Action()混淆了。Url.Action没有参数来设置目标,因为它仅返回URL。

根据您当前的代码,锚点可能看起来像:

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

127

只需使用,HtmlHelper ActionLink然后设置RouteValues和即可HtmlAttributes

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

22

由于UrlHelper.Action(string,string,object,object)不存在,因此无法编译。

UrlHelper.Action只会根据您提供的操作(而不是<a>标记)生成Urls 。如果要添加HtmlAttribute(如target="_blank",以在新标签页中打开链接),则可以:

  • <a>自己将target属性添加到元素:

    <a href="@Url.Action("RunReport", "Performance",
        new { reportView = Model.ReportView.ToString() })",
        target = "_blank" type="submit" id="runReport" class="button Secondary">
        @Reports.RunReport
    </a>
    
  • 使用Html.ActionLink生成<a>标记元素:

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

18

如果您的目标是使用ActionLink帮助器并打开一个新标签:

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

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

6

具有命名参数:

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

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

2
尽管此代码可以回答问题,但提供有关此代码为何和/或如何回答问题的其他上下文,可以提高其长期价值。
Alex Riabov

4

对于

@ Url.Action

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

1

asp.net mvc ActionLink带有角度参数的新选项卡

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

0

您将其设置typesubmit。这意味着浏览器应发布您的<form>数据发布到服务器。

实际上,根据w3schools,标签没有类型属性。

所以远程type属性,它应该为您工作。


0

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

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.