3

我有一个由标准页眉和页脚以及RenderBody()函数组成的布局页面。我的页眉包含一个导航,顶部带有大按钮作为系统中的模块

Customer    Quote    Jobs     etc

我的问题是我希望能够根据已单击的按钮在加载时为这些按钮提供一类活动。我是否需要在 javascript 中执行此操作,或者无论如何我可以将模型数据或一些变量传递给布局文件来设置它?

4

2 回答 2

5

我使用以下函数来实现这一点:

public static string MarkActive<TModel>(this HtmlHelper<TModel> html,string url)
{
    if (html.ViewContext.HttpContext.Request.Url.LocalPath
        .ToLower().StartsWith(url.ToLower().Trim()))
        return "active";
    else
        return null;
}

HtmlExtensions.cs这是我保存在文件中的扩展方法。我这样使用它:

<ul>
    <li class="@Html.MarkActive("/home")"><a href="/home">Home</a></li>
    <li class="@Html.MarkActive("/microsite"><a href="/microsite">View Microsite</a></li>
    <li class="@Html.MarkActive("/settings")"><a href="/settings/">Settings</a></li>
    <li><a href="/account/logout">Log out</a></li>
</ul>

可能有更好的解决方案,但这对我有用。简而言之,你应该检查HttpContext你的页面。

于 2013-01-24T13:03:57.057 回答
0

我使用以下方法突出显示选定的菜单项:

    <ul class="menu">
        @{Dictionary<string, string> menuList = new Dictionary<string, string>() { 
                { "Customer", "Customer" },
                { "Quote", "Quote" },
                { "Jobs", "Jobs" },
                { "etc", "etc" },
            };
        }
        @foreach (var item in menuList )
        { 
            <li @(Request.ServerVariables["SCRIPT_NAME"].Contains(item.Key) ? " class=active" : string.Empty)>
                <a href="/Home/@item.Key">@item.Value</a></li>
        }
    </ul>

其中 item.Key 用于操作名称,item.Value 用于菜单中的相应名称。

于 2013-01-24T13:51:15.093 回答