简体   繁体   English

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

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

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? 有人知道怎么做吗?

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

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

Looks like you are confusing Html.ActionLink() for Url.Action() . 看起来你混淆了Url.Action()的 Html.ActionLink () Url.Action has no parameters to set the Target, because it only returns a URL. Url.Action没有用于设置Target的参数,因为它只返回一个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>

That won't compile since UrlHelper.Action(string,string,object,object) doesn't exist. 由于UrlHelper.Action(string,string,object,object)不存在UrlHelper.Action(string,string,object,object)因此无法编译。

UrlHelper.Action will only generate Urls based on the action you provide, not <a> markup. UrlHelper.Action将仅根据您提供的操作生成Urls,而不是<a>标记。 If you want to add an HtmlAttribute (like target="_blank" , to open link in new tab) you can either: 如果要添加HtmlAttribute(如target="_blank" ,要在新选项卡中打开链接),您可以:

  • Add the target attribute to the <a> element by yourself: 自己将目标属性添加到<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生成<a>标记元素:

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

If your goal is to use the ActionLink helper and open a new tab: 如果您的目标是使用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"})

For 对于

@Url.Action @ Url.Action

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

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

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

You are setting it't type as submit . 要设置it't typesubmit That means that browser should post your <form> data to the server. 这意味着浏览器应该将<form>数据发布到服务器。

In fact a tag has no type attribute according to w3schools . 事实上,根据w3schools,标签没有类型属性。

So remote type attribute and it should work for you. 所以远程type属性,它应该适合你。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM