Welcome to this in-depth tutorial on View Components in ASP.NET Core MVC. View Components are powerful tools for encapsulating reusable UI logic, making your applications cleaner, more modular, and easier to maintain.
View Components are similar to partial views, but they are designed for more complex scenarios. They consist of a class (like a controller) and a view (like a partial view). They are ideal for:
Let's start by creating a simple View Component that displays a list of categories.
Create a new class, for example, CategoryList.cs
, in a ViewComponents
folder in your project.
public class CategoryList : ViewComponent
{
public IViewComponentResult Invoke()
{
// In a real application, you would fetch this data from a database or service.
var categories = new List<string> { "Electronics", "Books", "Clothing", "Home Goods" };
return View(categories);
}
}
Create a corresponding Razor view. The convention is to place it in Views/Shared/Components/CategoryList/Default.cshtml
.
@model IEnumerable<string>
<div class="category-widget">
<h4>Categories</h4>
<ul>
@foreach (var category in Model)
{
<li><a href="#">@category</a></li>
}
</ul>
</div>
// Optional: You can also specify a different view name if needed.
In your regular Razor view (e.g., Views/Home/Index.cshtml
), you can invoke the View Component using the <vc:category-list></vc:category-list>
tag helper.
@{
ViewData["Title"] = "Home Page";
}
<div class="text-center">
<h1 class="display-4">Welcome</h1>
<p>Learn about View Components in ASP.NET Core.</p>
<!-- Invoking the View Component -->
<vc:category-list></vc:category-list>
</div>
View Components can accept parameters to customize their behavior. The Invoke
method overload is used for this.
public class CategoryListWithCount : ViewComponent
{
public IViewComponentResult Invoke(int maxCount)
{
// Fetch data, applying the maxCount filter
var categories = new List<string> { "Electronics", "Books", "Clothing", "Home Goods", "Toys", "Groceries" }
.Take(maxCount)
.ToList();
return View(categories);
}
}
Invoke in the view:
<vc:category-list-with-count max-count="3"></vc:category-list-with-count>
If you need to return different views based on certain conditions, you can use named views.
public IViewComponentResult Invoke()
{
var hasData = true; // Example condition
if (hasData)
{
return View("Default", new[] { "Data1", "Data2" });
}
else
{
return View("Empty");
}
}
You would then create Empty.cshtml
in the same component folder.
You can use the [ViewComponent(Name = "CustomName")]
attribute to specify a different name for your View Component.