Search code examples
c#htmlasp.net-corerazorrazor-pages

How Can I add comma after "a" Tag in razor page ASP.NET Core 8


I'm trying to add comma(,) after "a" tag in foreach like this:

@foreach (var item in Model.Product.Categories)
{
    <a href="#" class="product-link product-cat-title">@item.Name</a>
    ,
}

but I'm getting error that says "} Expected". What should I do?

I've tested @string.join(", ", Model.Categories.Select(p => p.Name).ToArray()) but that will return string or text. I want Link!

It's worth mentioning that I don't want to add another tag Like <span> or <a>. I know they Work. And I'll appreciate if someone could explain why it doesn't work?

Thanks


Solution

  • https://learn.microsoft.com/en-us/aspnet/core/mvc/views/razor?view=aspnetcore-8.0#explicit-delimited-transition

    Use this approach to render HTML that isn't surrounded by an HTML tag. Without an HTML or Razor tag, a Razor runtime error occurs

    @foreach (var item in Model.Product.Categories)
    {
        <a href="#" class="product-link product-cat-title">@item.Name</a>
        <text >,</text>
    }