Roadmap › ASP.NET Core MVC (and Razor Pages) › Module: Razor Views
Lessons in this course (15)
10. Partial views vs view components
Intermediate★ Must knowLesson 10 of 15
| Partial view | View component | |
|---|---|---|
| Logic | None (just markup with a model you pass) | Has its own C# class with InvokeAsync — can call services |
| Data | Parent must supply the model | Fetches its own data |
| Use | Reusable markup fragments (product card) | Widgets: shopping cart summary, menu, recent posts |
| Invoke | <partial name="_Card" model="x" /> | @await Component.InvokeAsync("Cart") or <vc:cart></vc:cart> |
csharp
public class CartViewComponent(ICartService cart) : ViewComponent
{
public async Task<IViewComponentResult> InvokeAsync() => View(await cart.GetSummaryAsync(UserClaimsPrincipal));
}
// View: Views/Shared/Components/Cart/Default.cshtmlChild actions (@Html.Action) from MVC 5 no longer exist — view components replace them.