gpt4 book ai didi

asp.net-mvc-3 - ASP.NET MVC - 当前页面在导航中突出显示

转载 作者:行者123 更新时间:2023-12-03 15:06:59 24 4
gpt4 key购买 nike

我想知道如何在使用 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>

如您所见,我的导航中有 4 个链接,第一个链接应用了 CSS 类“当前”,我希望能够根据哪个页面将此类添加/删除到导航中的不同链接用户在。这可能吗?

干杯

最佳答案

你可以这样做

@{ 
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" : "" })

关于asp.net-mvc-3 - ASP.NET MVC - 当前页面在导航中突出显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9289909/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com