Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't 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 [email protected]</li>
}
</ul>

It treats [email protected] as a literal. How can I have a character placed right before the @ symbol?

like image 843
Dialetic Avatar asked Aug 22 '11 05:08

Dialetic


People also ask

What are code blocks in views Razor?

Code block is used to enclose C# code statements. It starts with @ (at) character and is enclosed by {} (curly braces). Unlike expressions, C# code inside code blocks is not rendered.

Does ASP.NET MVC use Razor?

Razor has no ties to ASP.NET MVC because Razor is a general-purpose templating engine. You can use it anywhere to generate output like HTML. It's just that ASP.NET MVC has implemented a view engine that allows us to use Razor inside of an MVC application to produce HTML.

What is the difference between Razor and Cshtml?

razor helps you embed serverside code like C# code into web pages. cshtml is just a file extension. razor view engine is used to convert razor pages(. cshtml) to html.


1 Answers

That scenario is there to allow [email protected] (emails) to exist unmolested in existing markup. Instead, use:

x@(product.Count)

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

or better html-wise:

&times;@(product.Count)

You might also then find this works conveniently:

&times;@product.Count
like image 97
Marc Gravell Avatar answered Sep 30 '22 15:09

Marc Gravell