In ASP.NET Core MVC, Partial Views are used to render a portion of a view content. They are especially useful for reusing view code (like a widget, a form, or a list) across multiple views.
Here’s a concise guide on how to use Partial Views in ASP.NET Core:
1. Create a Partial View
Partial views are just regular Razor view files, typically named with a leading underscore (e.g., _MyPartial.cshtml).
Place them in the Views/Shared folder (for global use) or in a specific controller’s view folder.
Example: Views/Shared/_ProductSummary.cshtml
@model MyApp.Models.Product
<div class="product-summary">
<h3>@Model.Name</h3>
<p>@Model.Description</p>
<span>Price: @Model.Price.ToString("C")</span>
</div>
2. Render a Partial View from a Parent View
You can render a partial view in a parent view using:
a. Html.Partial (synchronous, returns HTML string)
@Html.Partial("_ProductSummary", Model.Product)
b. Html.RenderPartial (synchronous, writes directly to output)
@{ Html.RenderPartial("_ProductSummary", Model.Product); }
c. Html.PartialAsync / Html.RenderPartialAsync (asynchronous)
@await Html.PartialAsync("_ProductSummary", Model.Product)
or
@{ await Html.RenderPartialAsync("_ProductSummary", Model.Product); }
Tip: Use the async versions in modern ASP.NET Core apps for better scalability.
3. Passing a Model to a Partial View
You can pass a model (or a subset of your main model) to the partial view as shown above.
Example:
@await Html.PartialAsync("_ProductSummary", product)
In the partial view, declare the model type:
@model MyApp.Models.Product
4. Partial Views with Collections
If you want to render a partial view for each item in a collection:
Parent View:
@foreach (var product in Model.Products)
{
@await Html.PartialAsync("_ProductSummary", product)
}
5. Partial Views via Controller (for AJAX, etc.)
You can return a partial view from a controller action, useful for AJAX scenarios:
public IActionResult ProductSummary(int id)
{
var product = _db.Products.Find(id);
return PartialView("_ProductSummary", product);
}
6. Using Tag Helpers (ASP.NET Core 2.1+)
You can also use the <partial> tag helper:
<partial name="_ProductSummary" model="product" />
Summary Table
| Method | Usage Example | Notes |
|---|
@Html.Partial | @Html.Partial("_Partial", model) | Synchronous, returns string |
@Html.RenderPartial | @{ Html.RenderPartial("_Partial", model); } | Synchronous, writes output |
@await Html.PartialAsync | @await Html.PartialAsync("_Partial", model) | Asynchronous, returns string |
@await Html.RenderPartialAsync | @{ await Html.RenderPartialAsync("_Partial", model); } | Asynchronous, writes output |
<partial> Tag Helper | <partial name="_Partial" model="model" /> | Preferred in Razor Pages |
Best Practices
- Use the async versions for better performance.
- Use the
<partial> tag helper for cleaner Razor syntax.
- Place reusable partials in
Views/Shared.
- Keep partials focused and small for reusability.
Let me know if you want a full example or have a specific scenario in mind!