我想知道在使用ASP.NET MVC 3时如何将CSS类添加到导航中的当前页面?这是我在_Layout.cshtml文件中的导航:
<p>@Html.ActionLink("Product Search", "Index", new { controller = "Home" }, new { @class = "current" })
| @Html.ActionLink("Orders", "Index", new { controller = "Orders" })
| @Html.ActionLink("My Account", "MyAccount", new { controller = "Account" })
| @Html.ActionLink("Logout", "LogOff", new { controller = "Account" })</p>
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,我的导航中有4个链接,第一个链接应用了CSS类"当前",我希望能够根据哪个页面将此类添加/删除到导航中的不同链接用户在.这可能吗?
干杯
dkn*_*ack 25
你可以这样做
@{
var currentController = ViewContext.RouteData.Values["controller"] as string ?? "Home";
var currentAction = ViewContext.RouteData.Values["action"] as string ?? "Index";
var currentPage = (currentController + "-" + currentAction ).ToLower();
}
@Html.ActionLink("Product Search", "Index", "Home", null,
new { @class = currentPage == "home-index" ? "current" : "" })
@Html.ActionLink("MyAccount", "MyAccount", "Account", null,
new { @class = currentPage == "account-myaccount" ? "current" : "" })
Run Code Online (Sandbox Code Playgroud)
bin*_*les 14
我建议使用扩展方法.就像是:
public static HtmlString NavigationLink(
this HtmlHelper html,
string linkText,
string actionName,
string controllerName)
{
string contextAction = (string)html.ViewContext.RouteData.Values["action"];
string contextController = (string)html.ViewContext.RouteData.Values["controller"];
bool isCurrent =
string.Equals(contextAction, actionName, StringComparison.CurrentCultureIgnoreCase) &&
string.Equals(contextController, controllerName, StringComparison.CurrentCultureIgnoreCase);
return html.ActionLink(
linkText,
actionName,
controllerName,
routeValues: null,
htmlAttributes: isCurrent ? new { @class = "current" } : null);
}
Run Code Online (Sandbox Code Playgroud)
然后,您可以通过包含扩展名称空间并只调用方法在View中使用它:
@using MyExtensionNamespace;
...
@Html.NavigationLink("Product Search", "Index", "Home")
| @Html.NavigationLink("Orders", "Index", "Orders")
| @Html.NavigationLink("My Account", "MyAccount", "Account")
| @Html.NavigationLink("Logout", "LogOff", "Account")
Run Code Online (Sandbox Code Playgroud)
这有利于保持剃须刀更清洁,并且可以在其他视图中轻松重复使用.
@{
var controller = ViewContext.RouteData.Values["controller"].ToString();
var action = ViewContext.RouteData.Values["action"].ToString();
var isActiveController = new Func<string, string, string, string, string>((ctrl, act, activeStyle, inactiveStyle) => controller == ctrl && action == act ? activeStyle : inactiveStyle);
}
Run Code Online (Sandbox Code Playgroud)
然后在HTML的class属性中,您可以执行以下操作:
class="@isActiveController("controlername","action","activecssstyleclass","inactiveccsstyle")"
Run Code Online (Sandbox Code Playgroud)
只是另一种方式@dknaack他的答案..更通用,更少的功能重复你的代码.
就我而言,假设我有一个主页和一个菜单。
ViewBag.Active在主页中添加 a作为占位符,如下所示:
@{
ViewBag.Title = "Home";
ViewBag.Active = "Home";
}
Run Code Online (Sandbox Code Playgroud)
然后将其放置到您的li类中作为激活或不激活它的条件:
<li class="@(ViewBag.Active=="Home"? "active" : "")">
<a href="@Url.Action("Index", "Home")"><span>@ViewBag.Title</span></a>
</li>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10252 次 |
| 最近记录: |