I'm building a Blazor page (as a learning project) and want to create a dynamic menu based on user permissions.
My table layout is
MenuItemID | ItemName | ParentID
I would like to load the information from this table and dynamically create the menu. I'm using MudBlazor for the menu layout.
In the standard Winforms i'm comfortable how to recursively call a function, but i'm not sure how to do this within Blazor to dynamically generate the HTML.
I have a function written for finding the items
private List<MenuItem> GetChildItems(int ParentID)
{
var foundItems = from m in menuItems
where m.ParentID == ParentID
select m;
return foundItems.ToList();
}
My thoughts are my code within my page would start with something like:
@if (menuItems == null)
{
<div class="spinner"></div>
}
else
{
foreach (MenuItem p in GetChildItems(0))
{
List<MenuItem>
children = GetChildItems(p.MenuItemID);
if (children.Count > 0)
{
<MudNavGroup Title="@p.ItemName" Icon="@Icons.Material.Outlined.AssignmentInd" HideExpandIcon="true">
@foreach (MenuItem c in children)
{
<MudNavLink Href="pages/authentication/login" Icon="@Icons.Material.Outlined.InsertDriveFile">@c.ItemName</MudNavLink>
}
</MudNavGroup>
}
else
{
<MudNavLink Href="/FetchData" Match="NavLinkMatch.All" Icon="@Icons.Material.Outlined.Dashboard">@p.ItemName</MudNavLink>
}
}
}
What i'm not 100% sure on is how I can convert this to a recursive search?
Thanks Stephen