Razor Syntax
Razor Syntax
Razor Syntax
Razor Syntax
Razor Syntax allows you to embed code (C#) into page @page
views through the use of a few keywords (such as “@”),
and then have the C# code be processed and
@model IndexModel
converted at runtime to HTML. In other words, rather <h2>Welcome</h2>
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
<ul>
dynamically generated HTML web page. @for (int i = 0; i < 3; i++)
{
<li>@i</li>
}
</ul>
https://www.codecademy.com/learn/learn-asp-net/modules/asp-net-razor-syntax/cheatsheet 1/10
19/01/2024, 13:08 Learn ASP.NET: ASP.NET: Razor Syntax Cheatsheet | Codecademy
The page model class, i.e. the data and methods that @page
hold the functionality associated with a view page, is
@model PersonModel
made available to the view page via the @model
directive.
By specifying the model in the view page, Razor exposes // Rendering the value of FirstName in
a Model property for accessing the model passed to
PersonModel
the view page. We can then access properties and
functions from that model by using the keyword <p>@Model.FirstName</p>
Model or render its property values on the browser
by prefixing the property names with @Model , e.g. <ul>
@Model.PropertyName .
// Accessing the value of FavoriteFoods
in PersonModel
@foreach (var food in
Model.FavoriteFoods)
{
<li>@food</li>
}
</ul>
Razor Markup
// Using parentheses:
<p>Last week this time: @(DateTime.Now -
TimeSpan.FromDays(7))</p>
https://www.codecademy.com/learn/learn-asp-net/modules/asp-net-razor-syntax/cheatsheet 2/10
19/01/2024, 13:08 Learn ASP.NET: ASP.NET: Razor Syntax Cheatsheet | Codecademy
Razor Conditionals
Conditionals in Razor code can be written pretty much // if-else if-else statment:
the same way you would in regular C# code. The only
@{ var time = 9; }
exception is to prefix the keyword if with the @
symbol. Afterward, any else or else if
conditions doesn’t need to be preprended with the @ @if (time < 10)
symbol. {
<p>Good morning, the time is: @time</p>
}
else if (time < 20)
{
<p>Good day, the time is: @time</p>
}
else
{
<p>Good evening, the time is: @time</p>
}
In Razor Pages, a switch statement begins with the @ @{ string day = "Monday"; }
symbol followed by the keyword switch . The
@switch (day)
condition is then written in parentheses and finally the
{
switch cases are written within curly brackets, {} .
case "Saturday":
<p>Today is Saturday</p>
break;
case "Sunday":
<p>Today is Sunday</p>
break;
default:
<p>Today is @day... Looking forward
to the weekend</p>
break;
}
https://www.codecademy.com/learn/learn-asp-net/modules/asp-net-razor-syntax/cheatsheet 3/10
19/01/2024, 13:08 Learn ASP.NET: ASP.NET: Razor Syntax Cheatsheet | Codecademy
https://www.codecademy.com/learn/learn-asp-net/modules/asp-net-razor-syntax/cheatsheet 4/10
19/01/2024, 13:08 Learn ASP.NET: ASP.NET: Razor Syntax Cheatsheet | Codecademy
https://www.codecademy.com/learn/learn-asp-net/modules/asp-net-razor-syntax/cheatsheet 5/10
19/01/2024, 13:08 Learn ASP.NET: ASP.NET: Razor Syntax Cheatsheet | Codecademy
In Razor Pages, you can use the ViewData property // Page Model: Index.cshtml.cs
to pass data from a Page Model to its corresponding
public class IndexModel : PageModel
view page, as well as share it with the layout, and any
partial views. {
ViewData is a dictionary that can contain key- public void OnGet()
value pairs where each key must be a string. The values
{
can be accessed in the view page using the @ symbol.
ViewData["Message"] = "Welcome to my
A huge benefit of using ViewData comes when
working with layout pages. We can easily pass page!";
information from each individual view page such as the ViewData["Date"] = DateTime.Now();
title , into the layout by storing it in the
}
ViewData dictionary in a view page:
}
@{ ViewData["Title"] = "Homepage"
}
We can then access it in the layout like so: // View Page: Index.cshtml
ViewData["Title"] . This way, we don’t need @page
to hardcode certain information on each individual
@model IndexModel
view page.
<h1>@ViewData["Message"]</h1>
<h2>Today is: @ViewData["Date"]</h2>
https://www.codecademy.com/learn/learn-asp-net/modules/asp-net-razor-syntax/cheatsheet 6/10
19/01/2024, 13:08 Learn ASP.NET: ASP.NET: Razor Syntax Cheatsheet | Codecademy
@{ Layout = "_LayoutExample" }
https://www.codecademy.com/learn/learn-asp-net/modules/asp-net-razor-syntax/cheatsheet 7/10
19/01/2024, 13:08 Learn ASP.NET: ASP.NET: Razor Syntax Cheatsheet | Codecademy
In Razor Pages, Tag Helpers change and enhance // Page Model: Example.cshtml.cs
existing HTML elements by adding specific attributes to
public class ExampleModel : PageModel
them. The elements they target are based on the
element name, the attribute name, or the parent tag. {
ASP.NET provides us with numerous built-in Tag Helpers public string Language { get; set; }
that can be used for common tasks - such as creating
forms, links, loading assets, and more.
public List<SelectListItem> Languages {
get; } = new List<SelectListItem>
{
new SelectListItem { Value = "C#",
Text = "C#" },
new SelectListItem { Value =
"Javascript", Text = "Javascript" },
new SelectListItem { Value = "Ruby",
Text = "Ruby" },
};
}
// HTML Rendered:
<form method="post">
<select id="Language" name="Language">
<option value="C#">C#</option>
<option
value="Javascript">Javascript</option>
<option value="Ruby">Ruby</option>
https://www.codecademy.com/learn/learn-asp-net/modules/asp-net-razor-syntax/cheatsheet 8/10
19/01/2024, 13:08 Learn ASP.NET: ASP.NET: Razor Syntax Cheatsheet | Codecademy
<br>
</select>
<button type="submit">Register</button>
</form>
Razor Partials
// Example.cshtml
<h1> Welcome to my page! </h1>
<h2> Fill out the form below to
subscribe!:</h2>
<partial name="_MyPartial" />
https://www.codecademy.com/learn/learn-asp-net/modules/asp-net-razor-syntax/cheatsheet 9/10
19/01/2024, 13:08 Learn ASP.NET: ASP.NET: Razor Syntax Cheatsheet | Codecademy
Print Share
https://www.codecademy.com/learn/learn-asp-net/modules/asp-net-razor-syntax/cheatsheet 10/10