C# 如何在新选项卡中打开剃刀操作链接?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10851860/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to have a a razor action link open in a new tab?
提问by FairyQueen
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?
但这不起作用。有人知道怎么做吗?
采纳答案by Erik Philips
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.
看起来您将Html.ActionLink()与Url.Action()混淆了。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>
回答by Sly
You are setting it't typeas submit. That means that browser should post your <form>data to the server.
您将其设置type为submit. 这意味着浏览器应该将您的<form>数据发布到服务器。
In fact a tag has no type attribute according to w3schools.
事实上,根据w3schools标签没有 type 属性。
So remote typeattribute and it should work for you.
所以远程type属性,它应该适合你。
回答by Gabe
Just use the HtmlHelperActionLinkand set the RouteValuesand HtmlAttributesaccordingly.
只需使用HtmlHelperActionLink和 相应地设置RouteValues和HtmlAttributes。
@Html.ActionLink(Reports.RunReport, "RunReport", new { controller = "Performance", reportView = Model.ReportView.ToString() }, new { target = "_blank" })
回答by WDRust
That won't compile since UrlHelper.Action(string,string,object,object)doesn't exist.
那不会编译,因为UrlHelper.Action(string,string,object,object)它不存在。
UrlHelper.Actionwill 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:
UrlHelper.Action只会根据您提供的操作而不是<a>标记生成网址。如果要添加 HtmlAttribute(例如target="_blank",在新选项卡中打开链接),您可以:
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" })
<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" })
回答by Faisal
<a href="@Url.Action("RunReport", "Performance", new { reportView = Model.ReportView.ToString() })" type="submit" id="runReport" target="_blank" class="button Secondary"> @Reports.RunReport </a>
<a href="@Url.Action("RunReport", "Performance", new { reportView = Model.ReportView.ToString() })" type="submit" id="runReport" target="_blank" class="button Secondary"> @Reports.RunReport </a>
回答by JoshYates1980
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"})
回答by usefulBee
With Named arguments:
使用命名参数:
@Html.ActionLink(linkText: "TestTab", actionName: "TestAction", controllerName: "TestController", routeValues: null, htmlAttributes: new { target = "_blank"})
回答by sanjeewa
asp.net mvc ActionLink new tab with angular parameter
asp.net mvc ActionLink 带有角度参数的新选项卡
<a target="_blank" class="btn" data-ng-href="@Url.Action("RunReport", "Performance")?hotelCode={{hotel.code}}">Select Room</a>
回答by Thisara Subath
For
为了
@Url.Action
@Url.Action
<a href="@Url.Action("Action", "Controller")" target="_blank">Link Text</a>
回答by Abiuth Arun
@Html.ActionLink(
"Pay Now",
"Add",
"Payment",
new { @id = 1 },htmlAttributes:new { @class="btn btn-success",@target= "_blank" } )

