Basic Razor Syntax
How to perform common logical tasks in Razor like if/else, foreach loops, switch statements and using the @ character to separate code and markup
The @ symbol
@* Writing a value inside a html element *@
<p>@Model.Name</p>
@* Inside an attribute *@
<a href="@Model.Url()">@Model.Name</a>
@* Using it to start logical structures *@
@if (selection?.Length > 0)
{
<ul>
@foreach (var item in selection)
{
<li>
<a href="@item.Url(PublishedUrlProvider)">@item.Name</a>
</li>
}
</ul>
}Embedding comments in razor
If/else
Foreach loops
Switch block
More information
Last updated
Was this helpful?