- I am using a bootstrap navbar in my _Layout.cshtml.
- On my Index page, the list item "Home" shows as
color: rgba(255, 140, 0, 0.781)
and the "Features" list item shows ascolor: green
Desired Result: When I click the "Features" link and navigate to the Features.cshtml
page, I want the "Features" list item to change color to color: rgba(255, 140, 0, 0.781)
and the Home item to change to color: green
.
This is easy to do if I put the navbar markup into every cshtml page. But I would like to just have my bootstrap navbar once in my _Layout.cshtml
page.
_Layout.cshtml
<nav class="navbar navbar-expand-lg navbar-light">
<div class="container">
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav mr-auto">
<li class="nav-item active">
<a class="nav-link" style="color: rgba(255, 140, 0, 0.781)" asp-page="Index")>@_loc["Home"]</a>
</li>
<li class="nav-item active">
<a class="nav-link" style="color: green" asp-page="Features" localize-content>@_loc["Features"]</a>
</li>
<partial name="_LoginPartial" />
</ul>
</div>
</div>
</nav>
I have this working fine by putting the HTML for the menu bar in every page, but that is not a good solution as I will have to separately maintain a number of instances of my menu bar.
I tried a number of stackoverflow items but didn't find one that worked for this case. Such as set Different colors in asp:DropDownList Items
I tried following the MSDocs for the ForeColor
property but couldn't achieve this either.
I have also tried using [ViewData]
set in my Index.cshtml.cs
but still couldn't figure out how to change the color on page load or when navigating to the Features page.
I have also tried adding @ code directly to my _Layout page, such as @if(this.Page = "Index")
and @if(System.IO.Directory.GetCurrentDirectory = "Index")
but no joy.