How to have a a razor action link open in a new ta

2020-02-08 06:23发布

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?

标签: c# html razor
10条回答
我欲成王,谁敢阻挡
2楼-- · 2020-02-08 07:02

For

@Url.Action

<a href="@Url.Action("Action", "Controller")" target="_blank">Link Text</a>
查看更多
Viruses.
3楼-- · 2020-02-08 07:06

asp.net mvc ActionLink new tab with angular parameter

<a  target="_blank" class="btn" data-ng-href="@Url.Action("RunReport", "Performance")?hotelCode={{hotel.code}}">Select Room</a>
查看更多
成全新的幸福
4楼-- · 2020-02-08 07:10
@Html.ActionLink(
"Pay Now",
"Add",
"Payment",
new { @id = 1 },htmlAttributes:new { @class="btn btn-success",@target= "_blank" } )
查看更多
Luminary・发光体
5楼-- · 2020-02-08 07:11

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.

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>
查看更多
够拽才男人
6楼-- · 2020-02-08 07:11

That won't compile since UrlHelper.Action(string,string,object,object) doesn't exist.

UrlHelper.Action will 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:

  • 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" })
    
查看更多
放我归山
7楼-- · 2020-02-08 07:18

Just use the HtmlHelper ActionLink and set the RouteValues and HtmlAttributes accordingly.

@Html.ActionLink(Reports.RunReport, "RunReport", new { controller = "Performance", reportView = Model.ReportView.ToString() }, new { target = "_blank" })
查看更多
登录 后发表回答