2012-06-01 11 views
44

ho cercando di ottenere il mio link per aprire in una nuova scheda (deve essere in formato rasoio):Come aprire un link di azione del rasoio in una nuova scheda?

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

Questo non funziona comunque. Qualcuno sa come fare questo?

+6

Perché dovresti preoccuparti di provare a inserirlo in "Url.Action"? Basta inserire il tag ' stesso. –

risposta

30

Sembra che tu stai confondendo Html.ActionLink() per Url.Action(). Url.Action non ha parametri per impostare la Target, perché restituisce solo un URL.

In base al codice corrente, l'ancora dovrebbe probabilmente essere simile:

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

Si sta impostando su type come submit. Ciò significa che il browser deve inviare i tuoi dati <form> al server.

Infatti un tag non ha attributo di tipo secondo w3schools.

Quindi attributo remoto type e dovrebbe funzionare per voi.

+1

Non ci si fida molto di [w3fools] (http://w3fools.com/) – dtsg

+1

sicuro, dovremmo tutti fidarci di IE :) – Sly

94

Basta usare il HtmlHelperActionLink e impostare il RouteValues e HtmlAttributes di conseguenza.

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

Questo è l'approccio consigliato se il collegamento è solo testo. –

16

Che non verrà compilato dal UrlHelper.Action(string,string,object,object) non esiste.

UrlHelper.Action genererà solo URL in base all'azione fornita, non <a> markup. Se si desidera aggiungere un HtmlAttribute (come target="_blank", ad aprire il collegamento in una nuova scheda) è possibile:

  • Aggiungere l'attributo target all'elemento <a> da soli:

    <a href="@Url.Action("RunReport", "Performance", 
        new { reportView = Model.ReportView.ToString() })", 
        target = "_blank" type="submit" id="runReport" class="button Secondary"> 
        @Reports.RunReport 
    </a> 
    
  • Usa Html .ActionLink per generare un elemento <a> markup:

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

Questa dovrebbe essere la risposta accettata – sveilleux2

0

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

14

Se il vostro obiettivo è quello di utilizzare l'helper ActionLink e aprire una nuova scheda:

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

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

Con argomenti con nome:

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

asp.net mvc ActionLink nuova scheda con il parametro angolare

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