Roadmap › ASP.NET Core MVC (and Razor Pages) › Module: Razor Views
Lessons in this course (15)
9. Tag helpers vs HTML helpers
Intermediate★ Must knowLesson 9 of 15
cshtml
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
<form asp-controller="Products" asp-action="Create" method="post">
<div asp-validation-summary="ModelOnly"></div>
<label asp-for="Name"></label>
<input asp-for="Name" class="form-control" />
<span asp-validation-for="Name"></span>
<select asp-for="CategoryId" asp-items="Model.Categories"></select>
<button type="submit">Save</button>
</form>
<a asp-action="Details" asp-route-id="@item.Id">View</a>
<img src="~/img/logo.png" asp-append-version="true" />
<environment include="Development"><script src="~/js/site.js"></script></environment>Old style HTML helpers: @Html.TextBoxFor(m => m.Name), @Html.ActionLink("View", "Details", new { id }). Tag helpers look like HTML, are designer-friendly, and the <form> tag helper adds the antiforgery token automatically.
Custom tag helper: a class deriving from TagHelper overriding Process(TagHelperContext, TagHelperOutput).