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

Fai*_*een 64 html c# razor

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

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

这不行.有人知道怎么做吗?

Gab*_*abe 117

只需使用HtmlHelper ActionLink和设置RouteValues,并HtmlAttributes相应地.

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


Eri*_*ips 38

看起来你混淆了Url.Action()的Html.ActionLink ().Url.Action没有用于设置Target的参数,因为它只返回一个URL.

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

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


WDR*_*ust 20

因为UrlHelper.Action(string,string,object,object)不存在,所以不会编译.

UrlHelper.Action只会根据您提供的操作生成Urls,而不是<a>标记.如果要添加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>
    
    Run Code Online (Sandbox Code Playgroud)
  • 使用Html.ActionLink生成<a>标记元素:

    @Html.ActionLink("Report View", "RunReport", null, new { target = "_blank" })
    
    Run Code Online (Sandbox Code Playgroud)


Jos*_*980 17

如果您的目标是使用ActionLink帮助程序并打开一个新选项卡:

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

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


小智 9

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

  • 虽然此代码可能会回答问题,但提供有关此代码为什么和/或如何回答问题的附加上下文可提高其长期价值。 (2认同)

Thi*_*ath 7

为了

@Url.Action

<a href="@Url.Action("Action", "Controller")" target="_blank">Link Text</a>
Run Code Online (Sandbox Code Playgroud)


use*_*Bee 5

具有命名参数:

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