CSharp-ASP-NET-Core-Razor-Views-and-Layouts
CSharp-ASP-NET-Core-Razor-Views-and-Layouts
SoftUni Team
Technical Trainers
Software University
https://softuni.bg
Table of Contents
2
Have a Question?
sli.do
#csharp-web
3
View Engine Essentials
View Engine Essentials
Views in ASP.NET Core MVC use the Razor View Engine to embed
.NET Code in HTML Markup.
Usually, they contain minimal logic, related only to presenting data
Data can be passed to a View by using the ViewData, ViewBag or
through a ViewModel (Strongly-Typed View).
5
Passing Data to a View
With ViewBag (dynamic type):
Action: ViewBag.Message = "Hello World!";
View: @ViewBag.Message
With ViewData (dictionary)
Action: ViewData["message"] = "Hello World!";
View: @ViewData["message"]
With Strongly-typed views:
Action: return View(model);
View: @model ModelDataType and then @Model.Message
6
Returning Views
The Base Controller class provides a lot of functionality
View() method – One of the most frequently used Controller class members
public class HomeController : Controller public IActionResult Index()
{ {
public IActionResult Index() return
{
this.View("~/Views/Other/Index.cshtml");
return this.View();
} }
}
7
How It Works?
Generated
Output
8
Razor Syntax
Razor Syntax
@ – For values (HTML encoded)
<p>
Current time is: @DateTime.Now!!!
Not HTML encoded value: @Html.Raw(someVar)
</p>
10
Razor Syntax (2)
If, else, for, foreach, etc. C# statements
HTML markup lines can be included at any part
@: – For plain text line to be rendered
<div class="products-list">
@if (Model.Products.Count() == 0) { <p>Sorry, no products found!</p> }
else
{
@:List of the products found:
foreach(var product in Model.Products)
{
<b>@product.Name, </b>
}
}
</div>
11
Razor Syntax (3)
Comments
@*
A Razor Comment
*@
@{
// A C# comment
/* A Multi
line C# comment
*/
}
<!-- HTML Comment -->
14
Layout and Special View Files
Layout
Define a common site template (~/Views/Shared/_Layout.cshtml)
Razor View engine renders content inside-out
First the View is rendered, and after that – the Layout
@RenderBody() –
indicate where we want
the views based on this
layout to "fill in" their
core content at that
location in the HTML
16
_ViewStart.cshtml
Views don't need to specify layout since their default layout is
set in their _ViewStart file:
~/Views/_ViewStart.cshtml (code for all views)
Each view can specify custom layout pages
@{
Layout = "~/Views/Shared/_UncommonLayout.cshtml";
}
23
Tag Helpers vs HTML Helpers
24
Creating Your Own Tag Helper
[HtmlTargetElement("h1")]
public class HelloTagHelper : TagHelper
{
private const string MessageFormat = "Hello, {0}";
public string TargetName { get; set; }
@using WebApplication;
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelper
@addTagHelper WebApplication.TagHelpersHelloTagHelper, WebApplication
<div class="tag-helper-content">
<h1 target-name="John"></h1>
</div>
25
Partial Views & View Components
Partial Views
Partial Views render portions of a page
Break up large markup files into smaller components
Reduce the duplication of common view code
Razor partial views are normal views (.cshtml files)
Usually placed in /Shared/ or in the same directory where used
Can be referenced with HTML helper or Tag Helper
Html helpers: Partial, PartialAsync, RenderPartial, etc.
Tag helper: <partial name="" model="" view-data="" for="" />
27
Use of Partial Views
HTML Helper for Partial Views
@using WebApplication.Models;
@model ProductsListViewModel
31
Defining Your Own ViewComponent
[ViewComponent(Name = "HelloWorld")]
public class HelloWorldViewComponent : ViewComponent
{
private readonly DataService dataService;
public HelloWorldViewComponent(DataService dataService)
{
this.dataService = dataService;
}
this.ViewData["Message"] = helloMessage;
this.ViewData["Name"] = name;
return View();
}
}
32
Defining Your Own ViewComponent
@* In Default.cshtml *@
<h1>@ViewData["Message"]!!! I am @ViewData["Name"]</h1>
<div class="view-component-content">
@await Component.InvokeAsync("HelloWorld", new { name = "David" });
<vc:HelloWorld name="John"></vc:HelloWorld>
</div>
33
Summary
Razor
… View Engine Essentials
Razor Syntax
…
Layout and Special Views
…
Sections
Tag Helpers & HTML Helpers
Partial Views & View Components
34
Questions?
© SoftUni – https://softuni.org. Copyrighted document. Unauthorized copy, reproduction or use is not permitted.
Trainings @ Software University (SoftUni)
Software University – High-Quality Education,
Profession and Job for Software Developers
softuni.bg, softuni.org
Software University Foundation
softuni.foundation
Software University @ Facebook
facebook.com/SoftwareUniversity
Software University Forums
forum.softuni.bg
3
License
37