Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Outputting literal HTML between razor statements results in compilation error

I have a foo object, and I want to output:

Title, Location

So I try...:

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

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

Both fail to compile.

However...:

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

...works.

Is there some trick I am missing?

Edit: This happens inside a codeblock, updated to reflect this.

like image 900
Kjensen Avatar asked Jan 21 '23 13:01

Kjensen


1 Answers

You could escape the , using the @: because the Razor parser considers it as part of the server side code and if you want to output the comma as is in the HTML it needs to be escaped:

@if (sometruestuff){
    @foo.Title@:, @foo.Location
}
like image 65
Darin Dimitrov Avatar answered Jun 04 '23 04:06

Darin Dimitrov