【问题标题】:MVC3 Razor Code Soup - Templating?MVC3 Razor Code Soup - 模板?
【发布时间】:2012-04-14 15:47:10
【问题描述】:

我正在尝试创建导航器以在某些页面上显示突出显示。我有以下内容:

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

我确定有更好的方法来减少汤汁吗?有人可以帮助初学者吗?

谢谢 詹姆斯伍德利

【问题讨论】:

    标签: c# asp.net-mvc-3 templates razor


    【解决方案1】:

    在我看来,为了避免您可以使用的潜在类似网址出错

    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)
    

    希望你能理解

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-10
      • 1970-01-01
      相关资源
      最近更新 更多