How it works...

A view component can replace a ChildAction or a Partial View. It includes more logic.

It will return HTML fragments, encoded or not, that will depend on what type we return from a view component (ViewComponentResult, ContentViewComponentResult, or HtmlContentViewComponentResult).

It can be synchronous or asynchronous.

It can access all the objects a controller can access (Request, RouteData, User, ViewBag, ModelState, and so on) and the parent View context (through the ViewContext object).

It can be called in a View, as a Partial View, or returned by a controller as a ViewComponentResult.

A ViewComponent is composed of:

  • A class that contains logic. This class can derive from the ViewComponent and/or the ViewComponentAttribute ...

Get ASP.NET Core MVC 2.0 Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.