I am new in ASP.NET Core. I have a navigation menu and I would like to track the active item. My idea is to use the action and controller names as navigation keys:
the problem is I don't know how to obtain the action and controller name in the _Layout.cshtml view...
I have tried the ViewContext.ActionDescriptor.DisplayName
but it renders something like this MyApp.Controllers.RecordsController.Index (MyApp)
I'd rather prefer to obtain something like this:
<script>$("li#@(Controller.Name)-@(Action.Name)")).addClass("active");</script>
You can achieve this server side if you combine this answer with the following.
Somewhere in your view.
On each
<li>
.Use
PS: Use ToLower() if required
Also you can activate your menu by style block
I would like to add something small that has perhaps been overlooked... Just add some embedded code in the layouts page at the top, before the HTML begins...
The code is pretty self-explanatory... Then call the variables' values from anywhere within your code just as they are, like the way I did as shown below:
I found this method to be much simpler and hassle-free than aforementioned methods. Cheerio ;)
With dot net core 2.0 for razor pages you could use like this
On the tags