2010-12-21 3 views
5

사용자가 Html.ActionLink()가 생성 한 앵커 태그에 해당 URL을 볼 수있게하고 싶습니다. 링크.ASP.NET MVC Html.ActionLink의 title 속성을 생성 된 URL로 설정하는 방법

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

가 어떻게 ActionLink 생성하는 것입니다 URL을 지정할 수 있습니다 : 이것은 제목 속성을 설정하지만, 그 값을 얻는 방법을 어디 붙어를 파악되는에 의해 이루어집니다? 내가 추측 한 것을 하드 코딩 할 수 있지만 그게 DRY을 위반합니다.

답변

5

당신은 링크를 생성하는 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) 
    } 
} 

을 이제 기본적으로 당신은 단순히이

같은 새로운 ActionLinkHelper 전화를해야합니다
<%= Html.ActionLinkWithTitle(@testrun.Name, "Download", "Trx", 
       new { path = @testrun.TrxPath }) %> 
2

Url.Action() 방법은

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

을 작동해야하지만 더 나은 방법이 있는지 확실하지 않습니다.

+0

DRY를 만족시키지 못합니다. 각 링크에 대해이 'new {title = Url.Action ("Download", "Trx")}를 수행해야합니다. –

4

jQuery를 해결할 수 있습니다.

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