向 Html.MenuItem 添加类

发布于 2024-12-25 10:53:28 字数 380 浏览 1 评论 0原文

我有一个由 MVC 2 生成的菜单项的 HTML 列表。

<%= Html.MenuItem("Home", "Home", "Index")%>
<%= Html.MenuItem("Login", "Login", "Account")%>

它以 HTML 形式生成:

<li>
  <a href="Index">Home</a>
</li>

<li>
  <a href="Account">Login</a>
</li>

如何为列表中的每个项目将 CSS 类添加到列表中的元素?

I've got a HTML list of menu items generated by MVC 2.

<%= Html.MenuItem("Home", "Home", "Index")%>
<%= Html.MenuItem("Login", "Login", "Account")%>

Which generate in HTML:

<li>
  <a href="Index">Home</a>
</li>

<li>
  <a href="Account">Login</a>
</li>

How can I add a CSS class to the element in the list for each item in the list?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

§普罗旺斯的薰衣草 2025-01-01 10:53:28

我猜测这个 MenuItem 是您编写的或从某人那里获取的扩展方法,我还猜测他们正在包装 ActionLink 方法,如:

public static string MenuItem(this HtmlHelper helper, string linkText, string actionName, string controllerName)
{
    string currentControllerName = (string)helper.ViewContext.RouteData.Values["controller"];
    string currentActionName = (string)helper.ViewContext.RouteData.Values["action"];

    // Add selected class
    if (currentControllerName.Equals(controllerName, StringComparison.CurrentCultureIgnoreCase) && currentActionName.Equals(actionName, StringComparison.CurrentCultureIgnoreCase))
        return string.Concat("<li class=\"selected\">", helper.ActionLink(linkText, actionName, controllerName), "</li>");

    // Add link
    return string.Concat("<li>", helper.ActionLink(linkText, actionName, controllerName), "</li>");
}

如果是这种情况,只需将 css 类作为参数并使用接受 htmlattributes 的 ActionLink,如下所示:

public static string MenuItem(this HtmlHelper helper, string linkText, string actionName, string controllerName, string cssClass = "menu-item")
{
    string currentControllerName = (string)helper.ViewContext.RouteData.Values["controller"];
    string currentActionName = (string)helper.ViewContext.RouteData.Values["action"];

    // Add selected class
    if (currentControllerName.Equals(controllerName, StringComparison.CurrentCultureIgnoreCase) && currentActionName.Equals(actionName, StringComparison.CurrentCultureIgnoreCase))
        return string.Concat("<li class=\"selected\">", helper.ActionLink(linkText, actionName, controllerName), "</li>");

    // Add link
    return string.Concat("<li>", helper.ActionLink(linkText, actionName, controllerName, new {@class = cssClass} ), "</li>");
}

然后你只需这样调用它们:

<%= Html.MenuItem("Home", "Home", "Index", "index-tem")%>

I am guessing this MenuItem is an extension method that you wrote or that you took from someone, I am also guessing that they are wrapping an ActionLink Method, As in:

public static string MenuItem(this HtmlHelper helper, string linkText, string actionName, string controllerName)
{
    string currentControllerName = (string)helper.ViewContext.RouteData.Values["controller"];
    string currentActionName = (string)helper.ViewContext.RouteData.Values["action"];

    // Add selected class
    if (currentControllerName.Equals(controllerName, StringComparison.CurrentCultureIgnoreCase) && currentActionName.Equals(actionName, StringComparison.CurrentCultureIgnoreCase))
        return string.Concat("<li class=\"selected\">", helper.ActionLink(linkText, actionName, controllerName), "</li>");

    // Add link
    return string.Concat("<li>", helper.ActionLink(linkText, actionName, controllerName), "</li>");
}

if this is the case just make it take is a css class as a parameter and use the ActionLink that takes in htmlattributes, as in:

public static string MenuItem(this HtmlHelper helper, string linkText, string actionName, string controllerName, string cssClass = "menu-item")
{
    string currentControllerName = (string)helper.ViewContext.RouteData.Values["controller"];
    string currentActionName = (string)helper.ViewContext.RouteData.Values["action"];

    // Add selected class
    if (currentControllerName.Equals(controllerName, StringComparison.CurrentCultureIgnoreCase) && currentActionName.Equals(actionName, StringComparison.CurrentCultureIgnoreCase))
        return string.Concat("<li class=\"selected\">", helper.ActionLink(linkText, actionName, controllerName), "</li>");

    // Add link
    return string.Concat("<li>", helper.ActionLink(linkText, actionName, controllerName, new {@class = cssClass} ), "</li>");
}

then you just call them like this:

<%= Html.MenuItem("Home", "Home", "Index", "index-tem")%>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文