[英]ASP.NET Core track active Page for sidebar using ViewComponent
this is the default view for my sidebar view component 这是我的侧边栏视图组件的默认视图
<li class="nav-header text-center pb-1 text-white"><strong>Menu de Navegação</strong></li>
@foreach (var item in Model.Items)
{
<li class="nav-item has-treeview">
<a href="@item.Url" class="nav-link custom-sidebar-link">
@Html.Raw(@item.Icon)
<p class="text-white">
@item.Nome
@if (item.SubItems != null)
{
<i class="fas fa-angle-left right"></i>
}
</p>
</a>
@if (item.SubItems != null)
{
<ul class="nav nav-treeview">
@foreach (var subItem in item.SubItems)
{
<li class="nav-item">
<a href="@subItem.Url" class="nav-link">
@Html.Raw(@subItem.Icon)
<p>
@subItem.Nome
</p>
</a>
</li>
}
</ul>
}
</li>
}
</ul>
</nav>
and this is the cs file 这是cs文件
public class SidebarAdminViewComponent : ViewComponent
{
public IViewComponentResult Invoke()
{
var claims = Request.HttpContext.User.Claims;
var role = claims.FirstOrDefault(c => c.Type == ClaimTypes.Role)?.Value; ;
var Sidebar = new SidebarViewModel();
Sidebar.Items = new List<SidebarItemViewModel>();
Sidebar.Items.Add(new SidebarItemViewModel
{
Nome = "Home",
Icon = "<i class='nav-icon fas fa-house'></i>",
Url = Url.Page("/Account/Home/Index")
});
Sidebar.Items.Add(sidebarItem);
return View(Sidebar);
}
}
as can you see there i don't have anything in my code to set an active class. 如您所见,我的代码中没有任何用于设置活动类的内容。 Well that's cause i have that handled right now with javascript 好吧,这是因为我现在已经用javascript处理了
$(function () {
var url = window.location;
// Adds active on inner anchor and treeview anchor and treeview menu-open state to li
$('ul.nav a').filter(function () {
return this.href == url;
}).addClass('active').parent().parent().siblings().addClass('active').parent().addClass('menu-open');
});
I would like to be able to do this just using C# but i have no idea how can i detect the current page the user is on. 我希望能够仅使用C#来执行此操作,但是我不知道如何检测用户所在的当前页面。
what you would do is to get the current page name from the view and add a comparison inside the loop you have (all inside the view) 您要做的是从视图中获取当前页面名称,并在您拥有的循环内添加一个比较(全部在视图内)
<li class="nav-header text-center pb-1 text-white"><strong>Menu de Navegação</strong></li>
@foreach (var item in Model.Items)
{
@* here for the parent menu item*@
<li class=@(ViewContext.RouteData.Values["Controller"].ToString().ToLower() == item.Name.ToLower() ? "nav-item has-treeview active" : "nav-item has-treeview">
<a href="@item.Url" class="nav-link custom-sidebar-link">
@Html.Raw(@item.Icon)
<p class="text-white">
@item.Nome
@if (item.SubItems != null)
{
<i class="fas fa-angle-left right"></i>
}
</p>
</a>
@if (item.SubItems != null)
{
<ul class="nav nav-treeview">
@foreach (var subItem in item.SubItems)
{
@* here for the child menu item*@
<li class=@(ViewContext.RouteData.Values["Action"].ToString().ToLower() == item.Url.Split('/').Last().ToLower() ? "nav-item active" : "nav-item">
<a href="@subItem.Url" class="nav-link">
@Html.Raw(@subItem.Icon)
<p>
@subItem.Nome
</p>
</a>
</li>
}
</ul>
}
</li>
}
</ul>
</nav>
I've used .ToString().ToLower()
because I was testing before posting, you might not need them 我使用了.ToString().ToLower()
因为我在发布前进行了测试,因此可能不需要它们
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.