Roadmap › ASP.NET Core MVC (and Razor Pages) › Module: Razor Views
Lessons in this course (15)
8. Razor syntax, layouts, sections and partial views
Basic★ Must knowLesson 8 of 15
cshtml
@model ProductListVm
@{ ViewData["Title"] = "Products"; }
<h1>@ViewData["Title"] (@Model.Items.Count)</h1>
@foreach (var p in Model.Items)
{
<partial name="_ProductCard" model="p" />
}
@if (!Model.Items.Any()) { <p>No products.</p> }
<p>Total: @Model.Items.Sum(i => i.Price).ToString("C")</p>
@* Razor comment *@
@section Scripts {
<partial name="_ValidationScriptsPartial" />
}cshtml
<!-- Views/Shared/_Layout.cshtml -->
<!DOCTYPE html>
<html>
<head><title>@ViewData["Title"] - MyShop</title></head>
<body>
<partial name="_Nav" />
<main>@RenderBody()</main>
@await RenderSectionAsync("Scripts", required: false)
</body>
</html>@outputs HTML-encoded values (XSS protection);@Html.Raw()disables encoding — avoid with user input._ViewStart.cshtmlsets the default layout;_ViewImports.cshtmlholds@usingand@addTagHelper.