Search code examples
c#razorblazornav

how to get NavLink inside of a list working?


This is the code I am working on, this is within a list

@foreach(var item in list){
<NavLink href=link>@item.Name</NavLink>
}

I would like link to be "mylink/item.Id", however I have tried the following with no success:

  1. Create a variable called link private string link = "mylink/" and then href = @(link +item.Id).
  2. href=@("mylink/+item.Id")
  3. href="mylink/{@item.Id}"
    

and a few others, but all I have gotten is an error "can't mix markup with c# code", so I am completely lost here, any help?

This is a Razor page, with Blazor


Solution

  • try this :

    @foreach(var item in list){
        var link = $"mylink/{item.Id}"; 
        <NavLink href=@link >@item.Name</NavLink>
    }