作为讨论的基础。 创建一个标准的ASP.NET MVC Web项目。
它将包含在母版页两个菜单项:
<div id="menucontainer">
<ul id="menu">
<li>
<%= Html.ActionLink("Home", "Index", "Home")%></li>
<li>
<%= Html.ActionLink("About", "About", "Home")%></li>
</ul>
</div>
如何设置指示当前页面的可视化CSS样式。 例如,在关于页/控制器的时候,我基本上是想做到这一点:
<%= Html.ActionLink("About", "About", "Home", new {class="current"})%></li>
而且,当然,在主页上:
<%= Html.ActionLink("Home", "Index", "Home", new {class="current"})%></li>
(有一个CSS样式名目前在视觉上表示的菜单,这是当前页。)
我可能爆发从母版页菜单DIV到内容占位符,但是这将意味着我必须把菜单的每个页面上。
任何想法,有没有一个很好的解决方案呢?
最简单的方法是让从ViewContext的的RouteData电流控制器和行动。 注意签名的变化,并使用@逃脱的关键字。
<% var controller = ViewContext.RouteData.Values["controller"] as string ?? "Home";
var action = ViewContext.RouteData.Values["action"] as string ?? "Index";
var page = (controller + ":" + action).ToLower();
%>
<%= Html.ActionLink( "About", "About", "Home", null,
new { @class = page == "home:about" ? "current" : "" ) %>
<%= Html.ActionLink( "Home", "Index", "Home", null,
new { @class = page == "home:index" ? "current" : "" ) %>
请注意,您可以结合这一个的HtmlHelper扩展如@乔恩的,并使其更清洁。
<%= Html.MenuLink( "About", "About", "Home", null, null, "current" ) %>
其中MenuActionLink是
public static class MenuHelperExtensions
{
public static string MenuLink( this HtmlHelper helper,
string text,
string action,
string controller,
object routeValues,
object htmlAttributes,
string currentClass )
{
RouteValueDictionary attributes = new RouteValueDictionary( htmlAttributes );
string currentController = helper.ViewContext.RouteData.Values["controller"] as string ?? "home";
string currentAction = helper.ViewContext.RouteData.Values["action"] as string ?? "index";
string page = string.Format( "{0}:{1}", currentController, currentAction ).ToLower();
string thisPage = string.Format( "{0}:{1}", controller, action ).ToLower();
attributes["class"] = (page == thisPage) ? currentClass : "";
return helper.ActionLink( text, action, controller, new RouteValueDictionary( routeValues ), attributes );
}
}
我最近创建的HTML助手这个看起来像:
public static string NavigationLink(this HtmlHelper helper, string path, string text)
{
string cssClass = String.Empty;
if (HttpContext.Current.Request.Path.IndexOf(path) != -1)
{
cssClass = "class = 'selected'";
}
return String.Format(@"<li><a href='{0}' {1}>{2}</a></li>", path, cssClass, text);
}
实施看起来是这样的:
<ul id="Navigation">
<%=Html.NavigationLink("/Path1", "Text1")%>
<%=Html.NavigationLink("/Path2", "Text2")%>
<%=Html.NavigationLink("/Path3", "Text3")%>
<%=Html.NavigationLink("/Path4", "Text4")%>
</ul>
如果你正在使用T4MVC,您可以使用此:
public static HtmlString MenuLink(
this HtmlHelper helper,
string text,
IT4MVCActionResult action,
object htmlAttributes = null)
{
var currentController = helper.ViewContext.RouteData.Values["controller"] as string ?? "home";
var currentAction = helper.ViewContext.RouteData.Values["action"] as string ?? "index";
var attributes = new RouteValueDictionary(htmlAttributes);
var cssClass = (attributes.ContainsKey("class"))
? attributes["class"] + " "
: string.Empty;
string selectedClass;
if(action.Controller.Equals(currentController, StringComparison.InvariantCultureIgnoreCase)
{
selectedClass = "selected-parent";
if(action.Action.Equals(currentAction, StringComparison.InvariantCultureIgnoreCase))
selectedClass = "selected";
}
cssClass += selectedClass;
attributes["class"] = cssClass;
return helper.ActionLink(text, (ActionResult)action, attributes);
}
这可能只是它的第五个参数,所以你的HTML属性角子空。 这里这篇文章描述它是这样,但你可以在四号arguement一些东西通过,第5是专门针对HTMLattributes
<script type="javascript/text">
$( document ).ready( function() {
@if (Request.Url.AbsolutePath.ToLower() == "/")
{
@Html.Raw("$('.navbar-nav li').eq(0).attr('class','active');")
}
@if (Request.Url.AbsolutePath.ToLower().Contains("details"))
{
@Html.Raw("$('.navbar-nav li').eq(1).attr('class','active');")
}
@if (Request.Url.AbsolutePath.ToLower().Contains("schedule"))
{
@Html.Raw("$('.navbar-nav li').eq(2).attr('class','active');")
}
});
</script>
在5分钟一起打发这一点,我大概可以重构它,但应该给你的基本概念,它可能是小网站最有用的。