Last active
August 29, 2018 20:37
-
-
Save hd9/975ee8b89847c4ace59348800fa93a8f to your computer and use it in GitHub Desktop.
Simplifying Razor logic with C# Local Functions in Asp.Net Core | blog.hildenco.com
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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