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

时间:2022-11-26 23:44:18

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()在将鼠标悬停在链接上时生成的锚标记的相应URL。这是通过设置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? I could hardcode something I guess but that violates DRY.

如何指定ActionLink将生成的URL?我可以硬编码我猜的东西,但这违反了DRY。

3 个解决方案

#1


5  

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

#2


4  

It is possible to solve jQuery.

有可能解决jQuery。

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

#3


2  

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.

但我不确定是否有更好的方法。

#1


5  

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

#2


4  

It is possible to solve jQuery.

有可能解决jQuery。

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

#3


2  

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.

但我不确定是否有更好的方法。