2010-12-21 30 views
5

Me gustaría que los usuarios puedan ver la URL correspondiente para una etiqueta de anclaje generada por Html.ActionLink() cuando se ciernen sobre el enlace. Esto se hace estableciendo el atributo título, pero cuando estoy atascado es encontrar la manera de conseguir ese valor:¿Cómo se establece el atributo de título de un ASP.NET MVC Html.ActionLink en la URL generada

@Html.ActionLink(@testrun.Name, "Download", "Trx", 
       new { path = @testrun.TrxPath }, new { title = ??) 

¿Cómo puedo especificar la dirección URL que ActionLink va a generar? Podría codificar algo, supongo, pero eso infringe DRY.

Respuesta

5

Usted podría utilizar Url.Action() para generar el enlace o se puede crear un método helper personalizado así:

public static class HtmlHelpers { 
    public static MvcHtmlString ActionLinkWithTitle(this HtmlHelper helper, 
                string linkText, 
                string actionName, 
                object routeValues) { 
     return helper.ActionLink(linkText, actionName, routeValues, 
       new {title = Url.Action(linkText, actionName, routevalues) 
    } 
} 

Ahora, básicamente, que sólo tendrá que llamar a su nueva ActionLinkHelper como esto

<%= Html.ActionLinkWithTitle(@testrun.Name, "Download", "Trx", 
       new { path = @testrun.TrxPath }) %> 
2

Url.Action() El método debería funcionar

@Html.ActionLink(@testrun.Name, "Download", "Trx", 
      new { path = @testrun.TrxPath }, new { title = Url.Action("Download", "Trx") }) 

Pero no estoy seguro de si hay una mejor manera.

+0

Pero no satisface DRY - tendrá que hacer esto 'new {title = Url.Action (" Descargar "," Trx ")}' para cada enlace. –

4

Es posible resolver jQuery.

<script type="text/javascript"> 
    $(function() { 
     $(selector).each(function() { 
      $(this).attr("title", $(this).attr("href")); 
     }); 
    }); 
</script> 
Cuestiones relacionadas