Why doesn't the razor detect this as a code block in ASP.NET MVC?

I have the following snippet:

<ul>
@foreach (var product in Model.Products)
{
    <li>@product.Name x@product.Count</li>
}
</ul>

It treats x@product.Count as a literal. How can I place the character before the @ character?

+5
source share
2 answers

This scenario allows you to allow foo@your.com(emails) to exist unhindered in existing markup. Use instead:

x@(product.Count)

(additional brackets immediately after @, i.e. @(...)indicate an explicit code expression)

or better html-wise:

&times;@(product.Count)

You can also find this conveniently:

&times;@product.Count
+14
source
<li>@product.Name x@(product.Count)</li>
+1
source

All Articles