liveBook · Manning

Partial tag helper

As explained in ASP.NET Core in Action, 3e

The Partial Tag Helper is an ASP.NET Core Tag Helper that renders a named partial view within a Razor file.

The Partial Tag Helper renders a named partial view inside a Razor file. Partial views split large pages into reusable HTML sections without handling requests, model binding, validation, or application logic. section 17.4.3

Render a reusable to-do item

Create _ToDo.cshtml with @model ToDoItemViewModel and markup for the title and tasks. A Razor Page can then render it for each recent item: <partial name="_ToDo" model="todo" />. section 17.4.3

Listing 17.12 Partial view _ToDo.cshtml for displaying a ToDoItemViewModel
@model ToDoItemViewModel                 #1
<h2>@Model.Title</h2>                    #2
<ul>                                     #2
    @foreach (var task in Model.Tasks)   #2
    {                                    #2
        <li>@task</li>                   #2
    }                                    #2
</ul>                                    #2

Shows the partial view’s model declaration and its Razor markup for displaying a to-do item. section 17.4.3

Listing 17.13 Rendering a partial view from a Razor Page
@page                                         #1
@model RecentToDoListModel                    #2
 
@foreach(var todo in Model.RecentItems)       #3
{
    <partial name="_ToDo" model="todo" />     #4
}

Shows a Razor Page looping through recent items and passing each item to the named partial. section 17.4.3

How ASP.NET Core in Action, 3e covers it

How deep it goes
Explains the purpose, boundaries, model passing, and named invocation, but stops short of broader configuration details.
Examples
Uses a reusable _ToDo.cshtml view and renders it for each item in a recent-to-dos list.
What it assumes
Assumes familiarity with Razor Pages and view models while distinguishing partial rendering from request handling.
What you can do after
Apply it via the Razor Partial Tag Helper to render reusable views with explicit models.
How it connects
Connects partial views with Razor Pages, Tag Helpers, layouts, and model rendering.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of ASP.NET Core in Action, 3e. The section links point to the parts of the book it draws on.