0

我想为<li class=''></li>项目添加一个 CSS 类。你能帮我吗?

public static string IsMenuActive(Blms.Model.MenuItem menuItem)
{
        string result = "";

        if (System.Web.HttpContext.Current.Request.ServerVariables["Url"].ToLower().IndexOf(GetMenuLink(menuItem).ToLower()) >= 0)
            result = "active";

        if (string.IsNullOrEmpty(result)) {
            foreach (var child in menuItem.Children)
            {
                if (System.Web.HttpContext.Current.Request.ServerVariables["Url"].ToLower().IndexOf(GetMenuLink(child).ToLower()) >= 0)
                {
                    result = "active";
                    break;
                }
            }
        }

        return result;
    }

HTML 方面:

 <li class="@Blms.Web.Core.MenuUtilities.GetMenuLiClass(menuItem) @Blms.Web.Core.MenuUtilities.IsMenuActive(menuItem)">
4

1 回答 1

1

尝试使用自定义 htmlhelper 来解决此问题。它将导致更清晰的视图。

https://www.simple-talk.com/dotnet/asp.net/writing-custom-html-helpers-for-asp.net-mvc/

于 2013-06-24T17:33:32.643 回答