【发布时间】:2015-06-16 07:42:04
【问题描述】:
我已经搜索并尝试了不同的方法,例如使用 JQuery Helper 类等,但我找不到适合我的场景的解决方案。我的代码在下面当我用户选择和页面刷新时我想要 Active li
<aside class="main-sidebar" style="background: #102C4B none repeat scroll 0% 0%;border-right: 1px solid #DCE1E4;">
<section class="sidebar">
<div>
<ul id="menu" class="sidebar-menu">
<li class=" treeview">
<a href="#">
<i style="color:#fff" class="fa fa-dashboard"></i> <span style="color: #fff;">Dashboard</span> <i style="color:#fff" class="fa fa-angle-left pull-right"></i>
</a>
<ul class="treeview-menu" style="background: #224775 none repeat scroll 0% 0%;">
@*<li class="active"><a style="color:#fff" href="~/Dashboard/Index">Dashboard</a></li>*@
@Html.MenuItem("Dashboard", "Index", "Dashboard")
</ul>
</li>
<li class="treeview">
<a href=" #">
<i style="color:#fff" class="fa fa-pie-chart"></i>
<span style="color: #fff;">Sales</span>
<i class="fa fa-angle-left pull-right" style="color:#fff"></i>
</a>
<ul class="treeview-menu" style="background: #224775 none repeat scroll 0% 0%;">
@*<li><a style="color:#fff" href="~/Sales/index"> View Sale</a></li>*@
@Html.MenuItem("View Sale", "index", "Sales")
</ul>
</li>
<li class="treeview">
<a href="#">
<i style="color:#fff" class="fa fa-laptop"></i>
<span style="color: #fff;">Invoice</span>
<i class="fa fa-angle-left pull-right" style="color:#fff"></i>
</a>
<ul class="treeview-menu" style="background: #224775 none repeat scroll 0% 0%;">
@*<li><a style="color:#fff" href="~/Invoice/Index"> View Invoice</a></li>
<li><a style="color:#fff" href="~/Invoice/Create">Add Invoice</a></li>*@
@Html.MenuItem("View Invoice", "Index", "Invoice")
@Html.MenuItem("Add Invoice", "Create", "Invoice")
</ul>
</li>
</ul>
</div>
</section>
</aside>
我正在使用的 Helper 类也在下面,但我无法让 li 保持打开状态。
using System;
using System.Web.Mvc;
using System.Web.Mvc.Html;
public static class Utilities
{
public static MvcHtmlString MenuItem(this HtmlHelper htmlHelper,
string text, string action,
string controller,
object routeValues = null,
object htmlAttributes = null)
{
var li = new TagBuilder("li");
var routeData = htmlHelper.ViewContext.RouteData;
var currentAction = routeData.GetRequiredString("action");
var currentController = routeData.GetRequiredString("controller");
if (string.Equals(currentAction,
action,
StringComparison.OrdinalIgnoreCase) &&
string.Equals(currentController,
controller,
StringComparison.OrdinalIgnoreCase))
{
li.AddCssClass("active");
}
if (routeValues != null)
{
li.InnerHtml = (htmlAttributes != null)
? htmlHelper.ActionLink(text,
action,
controller,
routeValues,
htmlAttributes).ToHtmlString()
: htmlHelper.ActionLink(text,
action,
controller,
routeValues).ToHtmlString();
}
else
{
li.InnerHtml = htmlHelper.ActionLink(text,
action,
controller).ToHtmlString();
}
return MvcHtmlString.Create(li.ToString());
}
}
【问题讨论】:
-
阿里,你测试我的答案了吗?
-
谢谢我今天检查了
标签: c# asp.net asp.net-mvc-4 c#-4.0