0

_Layout.cshtml で次のメニューを定義しています。これは、サイト内のすべてのページで共有されています。

<ul id="topnav" class="sf-menu">
    <li>@Html.ActionLink("Home", "Index", "Home", null, new { @class = Html.ActivePage("Home", "Index") })</li>
    <li>@Html.ActionLink("Services", "Services", "Home", null, new { @class = Html.ActivePage("Home", "Services") })
        <ul>
            <li>@Html.ActionLink("1", "1", "Services", null, new { @class = Html.ActivePage("Home", "Services") })</li>
            <li>@Html.ActionLink("2", "2", "Services", null, new { @class = Html.ActivePage("Home", "Services") })</li>
            <li>@Html.ActionLink("3", "3", "Services", null, new { @class = Html.ActivePage("Home", "Services") })</li>
            <li>@Html.ActionLink("3", "4", "Services", null, new { @class = Html.ActivePage("Home", "Services") })</li>
        </ul>
    </li>
    <li>@Html.ActionLink("FAQs", "FAQs", "Home", null, new { @class = Html.ActivePage("Home", "FAQs") })</li>
    <li>@Html.ActionLink("About", "About", "Home", null, new { @class = Html.ActivePage("Home", "About") })</li>
    <li>@Html.ActionLink("Contact", "Contact", "Home", null, new { @class = Html.ActivePage("Home", "Contact") })</li>
</ul>

私のRouteConfigは次のとおりです

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

routes.MapRoute(
    name: "Home",
    url: "{action}",
    defaults: new { controller = "Home", action = "Index" }
);

routes.MapRoute(
    name: "Services",
    url: "{controller}/{action}",
    defaults: new { controller = "Services", action = "Index" }
);

私のHTMLHelperは次のように定義されています

public static string ActivePage(this HtmlHelper helper, string controller, string action)
{
    string classvalue = "";
    string currentController = helper.ViewContext.Controller.ValueProvider.GetValue("controller").RawValue.ToString();
    string currentAction = helper.ViewContext.Controller.ValueProvider.GetValue("action").RawValue.ToString();
    if (currentController == controller && currentAction == action)
        classvalue = "active";
    return classvalue;
}

私ができるようにしたいのは、ユーザーが「サービス」メニュー項目が「アクティブ」としてマークされている「サービス」メニューのサブ項目のいずれかを選択したときです。最上位のメニュー項目を選択し、子項目を選択しないと、コードはそのまま機能します。

誰でもこれに関する提案を提供できますか。

TIA トレバー・ルイス。

4

0 に答える 0