The contents of a code block ({ ... }) are expected to be code, not markup.
If you want to put text directly in a code block, you have three choices:
- Wrap it in any HTML tag
- Wrap it in the special Razor
<text>tag, which will just render the text without the tag - Prepend the line with
@:, which is equivalent
Use the <text> tags
The <text> tag signals to the razor view engine to write the contents to the output.
@{ var foo = true; }
@if(foo) { <text>Yes</text> } else { <text>No</text> }
Use @:
Prefix the text with @: before the first instance of text in the line. The parser is interpreting everything following @:Yes... to be text.
@{ var foo = true;
if (foo)
{
@:Yes
}
else
{
@:No
}
}
Razor Syntax allows you to embed code (C#) into page views through the use of a few keywords (such as “@”), and then have the C# code be processed and converted at runtime to HTML. In other words, rather than coding static HTML syntax in the page view, a user can code in the view in C# and have the Razor engine convert the C# code into HTML at runtime, creating a dynamically generated HTML web page.
@page
@model IndexModel
<h2>Welcome</h2>
<ul>
@for (int i = 0; i < 3; i++)
{
<li>@i</li>
}
</ul>
Comments