selected 我猜你的意思是使用 CSS 突出显示,不是吗?如果是这种情况,我建议您编写一个自定义 HTML 帮助程序来生成这些链接:
public static IHtmlString 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 new HtmlString(li.ToString());
}
然后在你的布局中使用帮助器:
<ul>
@Html.MenuItem("link 1", "Action1", "Controller1")
@Html.MenuItem("link 2", "Action2", "Controller2")
...
</ul>
现在剩下的就是在你的 CSS 类中定义 .active 规则:
.active {
... something fancy to pop the currently selected link from the others
}