The Razor view engine in ASP.NET MVC 3, throws error when we specify the content block directly without enclosing it in tags especially inside the coding constructs like if, for loop and so on.
Following Snippet throws error
In such cases, developers usually specify tags like <span> as a workaround in order to specify the contents. But there exists two ways for directly represent this case.
This is a straight forward feature but unexposed to several developers. It is simple but effective.
Following Snippet throws error
@if(condition) { Any direct content....... }
In such cases, developers usually specify tags like <span> as a workaround in order to specify the contents. But there exists two ways for directly represent this case.
- @: - for indicating the characters following it are content items and usually used for representing single line content.
- <text>....</text> - entire content block is enclosed within the <text>....</text> element and usually used for representing multi line contents.
@if(condition) { <text>Multi line statement 1 Multi line statement 1 .................. Multi line statement n</text> } @if(condition) { @:single line content goes here.... }
This is a straight forward feature but unexposed to several developers. It is simple but effective.