确定新的MVC。我之前曾问过this问题并得到了答案,但我想知道是否有更简单的解决方案。
假设我有一个母版页,其中的菜单布局为无序列表。如何在当前选择的菜单项上设置CSS类?
编辑:
我正在使用菜单,就像启动新的MVC应用程序时开箱即用一样
<ul id="menu">
<li><%: Html.ActionLink("Home", "Index", "Home")%></li>
<li><%: Html.ActionLink("About", "About", "Home")%></li>
</ul>
最佳答案
Jakub Konecki的答案将我引向了正确的方向……这是我最终的 Controller Action :
[ChildActionOnly]
public ActionResult MainMenu()
{
var items = new List<MenuItem>
{
new MenuItem{ Text = "Home", Action = "Index", Controller = "Home", Selected=false },
new MenuItem{ Text = "My Profile", Action = "Index", Controller = "Profile", Selected = false},
new MenuItem{ Text = "About", Action = "About", Controller = "Home", Selected = false }
};
string action = ControllerContext.ParentActionViewContext.RouteData.Values["action"].ToString();
string controller = ControllerContext.ParentActionViewContext.RouteData.Values["controller"].ToString();
foreach (var item in items)
{
if (item.Controller == controller && item.Action == action)
{
item.Selected = true;
}
}
return PartialView(items);
}
希望这对某人有帮助。
关于asp.net - ASP.NET MVC菜单选定项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4653226/