A partial view is a small Razor template that renders a section of HTML inside another view or layout. Use one when the same markup appears in several places, or when a large view would be easier to manage as smaller pieces. A partial renders markup; it does not have its own PageModel or page handlers to handle requests. section 17.4.3 section 17.5
Reuse a to-do item
Suppose one page displays a single to-do item and another displays five recent items. Put the markup for one item in _ToDo.cshtml instead of copying it between pages. The partial declares @model ToDoItemViewModel, then uses @Model.Title and a loop over @Model.Tasks to produce the item's HTML. 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
The _ToDo.cshtml listing shows the model declaration and the markup generated for one to-do item. section 17.4.3
Render that partial with the <partial /> Tag Helper, supplying its name and the item to render: <partial name="_ToDo" model="todo" />. The recent-items page places this tag inside a loop, so each item supplies its own model. When given a name without an absolute path or file extension, Razor searches for the partial in the Pages folder, starting from the calling page. 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
}
The calling-page listing shows how a loop renders the same partial for each recent item. section 17.4.3
Choose a layout for HTML shared across whole pages, such as a header or footer; use a partial for a reusable piece within a page. A layout section supplies content to a named position in a layout, while a partial encapsulates markup that another view can render. _ViewStart.cshtml can set a default layout for Razor Pages, but it does not run for partial views. section 17.4 section 17.4.2 section 17.4.4
A partial in a layout
An ASP.NET Core Identity project template includes Pages/Shared/_LoginPartial.cshtml for Register and Login links. Its default _Layout.cshtml renders the partial, keeping those links in one reusable fragment. section 23.3.2


