Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you set the title attribute of an ASP.NET MVC Html.ActionLink to the generated URL

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.

like image 960
Keith Hill Avatar asked Dec 21 '10 22:12

Keith Hill


1 Answers

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 }) %>
like image 101
John Hartsock Avatar answered Sep 18 '22 18:09

John Hartsock