2012-04-14 73 views
1

我想創建我的導航器,以在某些頁面上顯示突出顯示。我有以下幾點:MVC3剃刀代碼湯 - 模板?

<li> 
    @if (Request.Url.AbsoluteUri.ToLowerInvariant().Contains("registration")) 
    { 
     @Html.ActionLink("Request an Invite", "index", "registration", null, new { @class = "active" }) 
    } 
    else 
    { 
     @Html.ActionLink("Request an Invite", "index", "registration") 
    } 
</li> 
<li> 
    @if (Request.Url.AbsoluteUri.ToLowerInvariant().Contains("login")) 
    { 
     @Html.ActionLink("Login", "index", "login", null, new { @class = "active" }) 
    } 
    else 
    { 
     @Html.ActionLink("Login", "index", "login") 
    } 
</li> 

我確定有一個更好的方法可以減少這個問題嗎?有人可以幫助初學者嗎?

感謝 詹姆斯·伍德利

回答

0

在我看來,以避免潛在的類似的URL錯誤,您可以使用

var contorller = @ViewContext.RouteData.Values["controller"] 
var action = @ViewContext.RouteData.Values["action"] 

然後讓你的代碼更小,我將使用一個內嵌條件

@Html.ActionLink("Request an Invite", "index", "registration", null, (controller = "Home" && action == "registration") ? new { @class = "active" } : null) 

我希望你有想法

+0

太好了 - 謝謝你! – 2012-04-17 09:50:53