如何有一个剃须刀行动链接打开在一个新的标签?

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

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

但是这样不行,有人知道怎么做吗?

140066 次浏览

你把 type设置为 submit,这意味着浏览器应该把你的 <form>数据发送到服务器。

In fact a tag has no type attribute according to W3学校.

So remote type attribute and it should work for you.

只需使用 HtmlHelper ActionLink并相应地设置 RouteValuesHtmlAttributes

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

看来你把 ActionLink ()呃,开拍搞混了。厄尔。操作没有设置 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>

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

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

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

    <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" })
    

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

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

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


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

带有命名论点:

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

Net mvc ActionLink 带角度参数的新选项卡

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

为了

@ Url 开始

<a href="@Url.Action("Action", "Controller")" target="_blank">Link Text</a>
@Html.ActionLink(
"Pay Now",
"Add",
"Payment",
new { @id = 1 },htmlAttributes:new { @class="btn btn-success",@target= "_blank" } )