MVC 3 Razor syntax for direct text output?

Using Razor, how can you write plain text without wrapping it in any html tag?

Example (this works, but adds extra span tags):

@{ var foo = true; } @if(foo) { <span>Yes</span> } else { <span>No</span> } 

I want my final markup to be as clean as possible and not have additional tags.

Thank!

+46
asp.net-mvc-3
Mar 27 '11 at 18:37
source share
2 answers

use the <text> tags

 @{ var foo = true; } @if(foo) { <text>Yes</text> } else { <text>No</text> } 

The <text> signals the razor viewer to write the output.

Alternatively you can use @:

 @{ var foo = true; } @if(foo) { @:Yes } else { @:No } 
+87
Mar 27 '11 at 18:38
source share

It is worth noting here:

@: can only be used internally @

(in case any body like me wonders why @: doesn't work!)

+7
Jul 09 '15 at 11:13
source share



All Articles