How to set title attribute ASP.NET MVC Html.ActionLink for generated url

I want users to be able to see the corresponding URL of the attached tag generated by Html.ActionLink () when they hover over the link. This is done by setting the title attribute, but where I'm stuck, it turns out how to get this value:

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

How can I specify the URL that ActionLink will generate? I could hardcode what I assume, but that violates DRY .

+5
source share
3 answers

You can use Url.Action () to create a link, or you can create a custom helper method, for example:

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

ActionLinkHelper,

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

jQuery.

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

Url.Action () 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 is a better way.

+2
source

All Articles