HTML letter output between razor instructions results in a compilation error

I have a foo object and I want to output:

Name, Location

So I'm trying ...:

@if (sometruestuff){ @foo.Title, @foo.Location } @if (sometruestuff){ @foo.Title , @foo.Location } 

Both will not compile.

But...

 @if (sometruestuff){ @foo.Title<span>,</span> @foo.Location } 

... works.

Is there any trick I'm missing?

Edit: this happens inside a code block updated to reflect this.

+4
source share
2 answers

You can avoid using @: because the Razor parser treats it as part of the server-side code, and if you want to output a comma, as it is in HTML, it needs to be escaped:

 @if (sometruestuff){ @ foo.Title@ :, @foo.Location } 
+9
source

You seem to be inside the code block, because otherwise it should work fine. You can try the following:

 <text> @foo.Title, @foo.Location </text> 
0
source

Source: https://habr.com/ru/post/1338556/


All Articles