Partial Views
A partial view is a piece of a page in its own .cshtml file, named with an underscore and shared by many views. Render it with the partial tag helper, passing it a model.
Journeyman · ASP.NET Core · Razor · Updated
After this lesson you can
- Create a partial view and render it with the partial tag helper, passing it a model
Key takeaways
- A partial view is a
.cshtmlfile holding part of a page; its name starts with an underscore, and one shared by several controllers lives inViews/Shared. <partial name="_ProductCard" model="product" />renders a partial where it stands, with its own model.- When the same HTML appears on more than one page, make it a partial: change one file, and every page that uses it changes.
- ASP.NET Core looks for a partial in the current controller's folder first, then in
Views/Shared.
The product list, the search results and the home page all show products as the same little card: name, price, an Out of stock badge, a Details button. Writing that card three times means three places to change it. A partial view writes it once.
A partial view
A partial view is an ordinary .cshtml file holding part of a page. By convention its name starts with an underscore, and a partial used by several controllers lives in Views/Shared. Here's Views/Shared/_ProductCard.cshtml:
@model Product
<div class="card h-100">
<div class="card-body">
<h2 class="h5 card-title">@Model.ProductName</h2>
<p class="card-text fs-5">@Model.UnitPrice?.ToString("C")</p>
@if (Model.UnitsInStock == 0)
{
<span class="badge text-bg-danger">Out of stock</span>
}
</div>
<div class="card-footer bg-transparent border-0">
<a class="btn btn-outline-primary btn-sm" asp-controller="Products" asp-action="Details" asp-route-id="@Model.ProductId">Details</a>
</div>
</div>
It has its own @model: one product. It knows nothing about the page around it.
Rendering it
The partial tag helper renders a partial view where it stands, and model passes what it should show:
@foreach (Product product in Model)
{
<div class="col">
<partial name="_ProductCard" model="product" />
</div>
}
The product list shows a card per product. The search results page uses the very same line. Change the card once, and both pages change.
ASP.NET Core finds _ProductCard the way it finds views: first in the current controller's folder, then in Views/Shared.
Partials you've already used
_ValidationScriptsPartial, from Client-Side Validation, is a partial: two script tags, shared by every form.- NorthwindWeb's layout shows the sign-in status with
<partial name="_LoginStatus" />, keeping the layout short.
Partial view, layout or view?
| Layout | View | Partial view | |
|---|---|---|---|
| Is | The frame around every page | One page's content | A piece used inside pages |
| Rendered by | _ViewStart choosing it |
An action's return View() |
<partial name="..." /> in another view |
| Example | _Layout.cshtml |
Products/Index.cshtml |
_ProductCard.cshtml |
Check yourself
1 question to try on the lesson page.
So what
When the same piece of HTML appears on more than one page, make it a partial: one file to change, with its own small model, dropped in wherever it's needed.
Key terms
- Partial View