【问题标题】:How to have a a razor action link open in a new tab?如何在新标签页中打开剃刀操作链接?
【发布时间】:2012-06-06 18:54:54
【问题描述】:

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

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

但这不起作用。有人知道怎么做吗?

【问题讨论】:

  • 您为什么要费心尝试将其放入Url.Action?只需放入 &lt;a&gt; 标签本身即可。

标签: c# html razor


【解决方案1】:

您没有将 type 设置为 submit。这意味着浏览器应该将您的&lt;form&gt; 数据发布到服务器。

事实上,根据w3schools,标签没有类型属性。

如此远程的type 属性,它应该对你有用。

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

      看起来您将Html.ActionLink()Url.Action() 混淆了。 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>
      

      【讨论】:

        【解决方案4】:

        由于UrlHelper.Action(string,string,object,object) 不存在,因此无法编译。

        UrlHelper.Action 只会根据您提供的操作生成 URL,而不是 &lt;a&gt; 标记。如果您想添加 HtmlAttribute(如 target="_blank",以在新选项卡中打开链接),您可以:

        • 自己给&lt;a&gt;元素添加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 生成一个&lt;a&gt; 标记元素:

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

        【讨论】:

          【解决方案5】:

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

          【讨论】:

            【解决方案6】:

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

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

            【讨论】:

              【解决方案7】:

              使用命名参数:

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

              【讨论】:

                【解决方案8】:

                asp.net mvc ActionLink 带有角度参数的新标签

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

                【讨论】:

                  【解决方案9】:

                  对于

                  @Url.Action

                  <a href="@Url.Action("Action", "Controller")" target="_blank">Link Text</a>
                  

                  【讨论】:

                    【解决方案10】:
                    @Html.ActionLink(
                    "Pay Now",
                    "Add",
                    "Payment",
                    new { @id = 1 },htmlAttributes:new { @class="btn btn-success",@target= "_blank" } )
                    

                    【讨论】:

                    • 虽然此代码可能会回答问题,但提供有关此代码为何和/或如何回答问题的额外上下文可提高其长期价值。
                    猜你喜欢
                    • 2013-11-06
                    • 2020-07-27
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 2013-11-30
                    • 1970-01-01
                    • 2011-09-29
                    相关资源
                    最近更新 更多