Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Simplifying Razor logic with C# Local Functions in Asp.Net Core | blog.hildenco.com
@using Microsoft.AspNetCore.Mvc.Controllers
@{
var controllerName = ((ControllerActionDescriptor)ViewContext.ActionDescriptor).ControllerName;
string GetClass(string controller)
{
return $"nav-link { (controllerName == controller ? "active admin" : "" ) }";
}
}
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<a class="p-2 bg-info text-white mr-3" asp-area="Admin" asp-controller="Home" asp-action="Index">Admin</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarColor01" aria-controls="navbarColor01" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse">
<ul class="navbar-nav mr-auto">
<li class="nav-item">
<a class="@GetClass("Properties")" asp-controller="Properties" asp-action="Index">Properties</a>
</li>
<li class="nav-item">
<a class="@GetClass("Meetings")" asp-controller="Meetings" asp-action="Index">Meetings</a>
</li>
<li class="nav-item">
<a class="@GetClass("Users")" asp-controller="Users" asp-action="Index">Users</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#" onclick="alert('todo'); return false;">Action Items</a>
</li>
<li class="nav-item">
<a class="nav-link" href="#" onclick="alert('todo'); return false;">Communications</a>
</li>
</ul>
</div>
</nav>
@*
*********************************************
Full blog post available at blog.hildenco.com
*********************************************
*@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.