5

I would like users to be able to see the corresponding URL for an anchor tag generated by Html.ActionLink() when they hover over the link. This is done by setting the title attribute but where I'm stuck is figuring out how to get that value:

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

How can I specify the URL that ActionLink is going to generate? I could hardcode something I guess but that violates DRY.

John Hartsock
  • 85,422
  • 23
  • 131
  • 146
Keith Hill
  • 194,368
  • 42
  • 353
  • 369

3 Answers3

5

You could use Url.Action() to generate the Link or you could Create a Custom Helper Method like this:

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 )
    }
}

Now basically you will simply need to call your new ActionLinkHelper like this

<%= Html.ActionLinkWithTitle(@testrun.Name, "Download", "Trx", 
                 new { path = @testrun.TrxPath }) %>
John Hartsock
  • 85,422
  • 23
  • 131
  • 146
4

It is possible to solve jQuery.

<script type="text/javascript">
    $(function () {
        $(selector).each(function () {
            $(this).attr("title", $(this).attr("href"));
        });
    });
</script>
takepara
  • 10,413
  • 3
  • 34
  • 31
2

The Url.Action() method should work

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

But I'm not sure if there's a better way.

David Glenn
  • 24,412
  • 19
  • 74
  • 94