Last post Jun 28, 2019 03:10 PM by talldaniel
Jun 28, 2019 01:51 PM|talldaniel|LINK
I asked this question
but doing as the answer suggests seems to be less obvious than it at first appeared.
If I am writing code in the _Layout file, how do I access the database?
Also, I found this link,
and the last coding example at the bottom of the page shows them using @If(User.IsInRole("Admin") and that is the part that is not working correctly for me, so I cannot use this example.
I have pasted the example code from the link. For my _Layout file, , the "@If(User.IsInRole("Admin") does not appear to work consistently for my page, which is shown (and presumably works as expected) in this example.
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li><a asp-area="" asp-controller="Home" asp-action="Index">Home</a></li>
var UserRoles = "";
UserRoles = "Admin";
UserRoles = "Manager";
@foreach (var menuNames in menus.GetMenuMaster(@UserRoles).Where(n => n.Parent_MenuID == "*"))
<a asp-area="" asp-controller=@menuNames.MenuURL asp-action=@menuNames.MenuFileName>@menuNames.MenuName</a>
@foreach (var subMenu in menus.GetMenuMaster(@UserRoles).Where(n => n.Parent_MenuID == @menuNames.MenuID))
<a asp-area="" asp-controller=@subMenu.MenuURL asp-action=@subMenu.MenuFileName>@subMenu.MenuName</a>
Jun 28, 2019 03:04 PM|mgebhard|LINK
The logic should not exist in a View. Look into View Components as that's what View Components are for.
Once you get something working, reading the DB and returning results, then you can look into caching the menu by user and role.
Jun 28, 2019 03:10 PM|talldaniel|LINK
This looks perfect.