Partial views are a powerful feature in ASP.NET Core MVC that allow you to break down your views into smaller, reusable components. This promotes modularity, improves maintainability, and makes it easier to manage complex UIs.
A partial view is essentially a view without a layout. They can be rendered within other views to display specific sections of content. Common use cases include:
To create a partial view, you simply create a new Razor file (.cshtml
) in your Views
folder, typically in a subfolder named Partials
or within the view folder of the specific controller.
Let's create a partial view that displays a product's name and price.
Views/Partials/_ProductCard.cshtml
<div class="product-card">
<h3>@Model.Name</h3>
<p>Price: <span class="price">@Model.Price.ToString("C")</span></p>
</div>
<style>
.product-card {
border: 1px solid #ccc;
padding: 15px;
margin-bottom: 15px;
border-radius: 5px;
background-color: #fafafa;
box-shadow: 0 2px 5px rgba(0,0,0,0.05);
}
.product-card h3 {
margin-top: 0;
color: #333;
}
.product-card .price {
color: #d1343c;
font-weight: bold;
}
</style>
You can render partial views from your controller actions or from within other views using the Html.Partial()
or Html.RenderPartialAsync()
helper methods.
In your main view (e.g., Views/Products/Index.cshtml
), you can render the partial view like this:
Views/Products/Index.cshtml
@model IEnumerable<YourApp.Models.Product>
<h2>Our Products</h2>
<div class="product-list">
@foreach (var product in Model)
{
@Html.Partial("_ProductCard", product)
@* Or for asynchronous rendering:
@await Html.RenderPartialAsync("_ProductCard", product)
*@
}
</div>
<style>
.product-list {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
gap: 20px;
}
</style>
The Html.Partial()
method takes the name of the partial view (prefixed with _
by convention) and optionally a model object to pass to it. If no model is specified, it inherits the model of the parent view.
You can also return a partial view directly from a controller action, often used for AJAX requests.
Controllers/ProductsController.cs
using Microsoft.AspNetCore.Mvc;
using YourApp.Models;
using System.Collections.Generic;
public class ProductsController : Controller
{
public IActionResult Index()
{
var products = new List<Product>
{
new Product { Id = 1, Name = "Laptop", Price = 1200.50m },
new Product { Id = 2, Name = "Mouse", Price = 25.99m },
new Product { Id = 3, Name = "Keyboard", Price = 75.00m }
};
return View(products);
}
// Action to return a partial view for a specific product (e.g., via AJAX)
public IActionResult ProductDetailsPartial(int id)
{
var product = new Product { Id = id, Name = "Awesome Gadget", Price = 99.99m }; // In a real app, fetch from DB
return PartialView("_ProductCard", product);
}
}
While both are used for reusable UI, View Components offer more capabilities:
Partial views are generally simpler and are good for rendering fragments of the current view's model.
You can pass data to partial views in several ways:
ViewBag
or ViewData
: These can be accessed within the partial view if they are populated in the parent view or controller.Tip: By convention, partial view filenames start with an underscore (_
), like _ProductCard.cshtml
. This helps distinguish them from regular views.
Partial views are a fundamental tool for building maintainable and modular ASP.NET Core MVC applications. By encapsulating UI logic into smaller, reusable pieces, you can significantly improve the development workflow and the overall structure of your web application.