MVC2中的活动视图的突出显示? [英] Highlight of active view in MVC2?
本文介绍了MVC2中的活动视图的突出显示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
好吧,我的网站是 www.kristianbak.com 。
我有一个css类 activebutton
。我想要在其他视图处于活动状态时进行更改。
任何有好主意的人?
解决方案
您可以测试当前操作,如果匹配应用一个CSS类:
%if(ViewContext.RouteData.GetRequiredString(action)==About){%>
...在此突出显示
<%}%>
更好的办法是编写一个HTML帮助器来生成菜单:
public static MvcHtmlString MenuItem(
this HtmlHelper htmlHelper,
string text,
string action,
string controller
)
{
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);
}
li.InnerHtml = htmlHelper.ActionLink(text,action,controller).ToHtmlString();
return MvcHtmlString.Create(li.ToString());
}
,然后使用它:
< ul>
<%= Html.MenuItem(Home,Home,Home)%>
<%= Html.MenuItem(About Me,About,Home)%>
<%= Html.MenuItem(My Work,Work,Home)%>
<%= Html.MenuItem(Blog,Index,Blog)%>
...
< / ul>
这将添加 active
如果当前请求匹配链接的操作和控制器,则锚定
Okay, my site is www.kristianbak.com.
I have a css class called activebutton
. I want that to change whenever another View is active. Currently it's merely hard-coded in the HTML (sitemaster).
Anyone with a good idea?
解决方案
You could test the current action and if it matches apply a CSS class:
<% if (ViewContext.RouteData.GetRequiredString("action") == "About") { %>
... highlight here
<% } %>
Even better I would write a HTML helper to generate the menu:
public static MvcHtmlString MenuItem(
this HtmlHelper htmlHelper,
string text,
string action,
string controller
)
{
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");
}
li.InnerHtml = htmlHelper.ActionLink(text, action, controller).ToHtmlString();
return MvcHtmlString.Create(li.ToString());
}
and then use it like this:
<ul>
<%= Html.MenuItem("Home", "Home", "Home") %>
<%= Html.MenuItem("About Me", "About", "Home") %>
<%= Html.MenuItem("My Work", "Work", "Home") %>
<%= Html.MenuItem("Blog", "Index", "Blog") %>
...
</ul>
This will add the active
class to the anchor if the current request matches the action and controller of the link.
这篇关于MVC2中的活动视图的突出显示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文