I - ASP - NET - Razor Syntax Cheatsheet - Codecademy
I - ASP - NET - Razor Syntax Cheatsheet - Codecademy
NET I
<ul>
@for (int i = 0; i < 3; i++)
{
<li>@i</li>
}
</ul>
<ul>
// Accessing the value of FavoriteFoods in PersonModel
@foreach (var food in Model.FavoriteFoods)
{
<li>@food</li>
}
</ul>
Razor Markup
Razor pages use the @ symbol to transition from HTML to C#. C# expressions are
evaluated and then rendered in the HTML output. You can use Razor syntax under the @page
following conditions: @model PersonModel
// Using parentheses:
<p>Last week this time: @(DateTime.Now - TimeSpan.FromDays(7))
</p>
Razor Conditionals
Conditionals in Razor code can be written pretty much the same way you would in
regular C# code. The only exception is to pre x the keyword if with the @ symbol. // if-else if-else statment:
Afterward, any else or else if conditions doesn’t need to be preprended with the @ @{ var time = 9; }
symbol.
<h1>@ViewData["Message"]</h1>
<h2>Today is: @ViewData["Date"]</h2>
Razor Shared Layouts
In Razor Pages, you can reduce code duplication by sharing layouts between view pages.
A default layout is set up for your application in the _Layout.cshtml le located in // Layout: _LayoutExample.cshtml
Pages/Shared/. <body>
Inside the _Layout.cshtml le there is a method call: RenderBody() . This method ...
speci es the point at which the content from the view page is rendered relative to the
<div class="container body-content">
layout de ned.
@RenderBody()
If you want your view page to use a speci c Layout page you can de ne it at the top by
<footer>
specifying the lename without the le extension: @{ Layout = "LayoutPage" }
<p>@DateTime.Now.Year - My ASP.NET Application</p>
</footer>
</div>
</body>
@{ Layout = "_LayoutExample" }
// 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>
<br>
</select>
<button type="submit">Register</button>
</form>
Razor View Start File
When creating a template with ASP.NET, a ViewStart.cshtml le is automatically
generated under the /Pages folder. // ViewStart.cshtml
The ViewStart.cshtml le is generally used to de ne the layout for the website but can @{
be used to de ne common view code that you want to execute at the start of each Layout: "_Layout"
View’s rendering. The generated le contains code to set up the main layout for the }
application.
Razor Partials
Partial views are an e ective way of breaking up large views into smaller components
and reduce complexity. A partial consists of fragments of HTML and server-side code to // _MyPartial.cshtml
be included in any number of pages or layouts. <form method="post">
We can use the Partial Tag Helper, <partial> , in order to render a partial’s content in a <input type="email" name="emailaddress">
view page.
<input type="submit">
</form>
// Example.cshtml
<h1> Welcome to my page! </h1>
<h2> Fill out the form below to subscribe!:</h2>
<partial name="_MyPartial" />