简体   繁体   English

如何将ASP.NET MVC Html.ActionLink的title属性设置为生成的URL

[英]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. 我希望用户能够看到Html.ActionLink()在将鼠标悬停在链接上时生成的锚标记的相应URL。 This is done by setting the title attribute but where I'm stuck is figuring out how to get that value: 这是通过设置title属性来完成的,但我遇到的问题是如何获取该值:

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

How can I specify the URL that ActionLink is going to generate? 如何指定ActionLink将生成的URL? I could hardcode something I guess but that violates DRY . 我可以硬编码我猜的东西,但这违反了DRY

You could use Url.Action() to generate the Link or you could Create a Custom Helper Method like this: 您可以使用Url.Action()生成链接,也可以创建自定义帮助方法,如下所示:

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 现在基本上你只需要像这样调用你的新ActionLinkHelper

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

It is possible to solve jQuery. 有可能解决jQuery。

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

The Url.Action() method should work Url.Action()方法应该有效

@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. 但我不确定是否有更好的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM