I have a little Blazor app (server side in the unlikely case that's relevant) in which I want to dynamically create some links based on fields of objects in a list - one field is the URL that the link should direct to and another field is just the text that should be shown on the NavLink component. So this is how this class looks like (simplified for this purpose):
public class portfolioname
{
public string name { get; set; }
public string url { get; set; }
}
In my app I create the objects and populate the list, which is called porturls - everything works fine.
The relevant part of my Razor page (which is just a modified version of the MS Sample Blazor app) then looks like this:
@for (int i = 0; i < porturls.Count; i++)
{
<li class="nav-item px-3">
<NavLink class="nav-link" href=@porturls[i].url Match=@NavLinkMatch.All>
@porturls[i].name
</NavLink>
</li>
}
This causes an index out of range error on the @porturls[i].name
line.
However everything works fine when I either simply leave out the @porturls[i].name
line (the links work taking the correct url from @porturls[i].url
there is just no text shown) or when I replace the NavLink
components by standard html <a href=@porturls[i].url>
.
Any advice what I'm doing wrong or what's the issue with the NavLink component here? Not sure what else I could try, I would prefer to use the NavLink component here.