In the previous unit, you created a Razor Page that displays a list of pizzas. You used the @
symbol to switch contexts between HTML and C#. In this unit, you’ll learn about tag helpers. Tag helpers are a special kind of HTML element that can contain C# code. You’ll also learn about page handlers. Page handlers are methods that handle browser requests. You’ll use page handlers in the next unit to add and delete pizzas.
Tag helpers
Tag helpers are used to address the inefficiencies of context switching between HTML and C#. Most of ASP.NET Core’s built-in Tag helpers extend standard HTML elements. Tag helpers provide extra server-side attributes for HTML elements, making the elements more robust.
There are four tag helpers you should know for this project: Partial, Label, Input, and Validation Summary Message.
Partial Tag Helper
CSHTMLCopy
<partial name="_ValidationScriptsPartial" />
This injects the contents of the _ValidationScriptsPartial.cshtml
file into a page. The _ValidationScriptsPartial.cshtml
file contains JavaScript that’s used to validate form input, so it needs to be included on every page that contains a form.
Label tag helper
CSHTMLCopy
<label asp-for="Foo.Id" class="control-label"></label>
This extends the standard HTML <label>
element. Like many tag helpers, it uses an asp-for
attribute. The attribute accepts a property from the PageModel
. In this case, the name of the PageModel
‘s Foo.Id
property (specifically, the string "Id"
) will be rendered as the content for an HTML <label>
element.
Input tag helper
CSHTMLCopy
<input asp-for="Foo.Id" class="form-control" />
Similar to the previous example, this extends the standard HTML <input>
element. It also uses an asp-for
attribute to specify a PageModel
property. In this case, the value of the Foo.Id
property will be rendered as the value
attribute for an HTML <input>
element.
Validation Summary Tag Helper
CSHTMLCopy
<div asp-validation-summary="All"></div>
oracle certification malaysia