How it works...

A view component/controller is created as a controller inheriting from the Controller class. It can be a hybrid object if:

  • It's decorated by the ViewComponent attribute with a ViewComponent name as parameter:
[ViewComponent(Name = "BasketComponent")] 
public class ProductController : Controller 
  • It contains an Invoke() method that returns an IViewComponentResult. The ViewData property of the ViewViewComponent object returned is the name of the .cshtml view component:
public IViewComponentResult Invoke() 
{ 
  List<string> listeData = new List<string>(); 
  return new ViewViewComponentResult() 
  { 
    ViewName = "BasketData", 
    ViewData = new ViewDataDictionary<IEnumerable<string>> 
    (ViewData, listeData) 
  }; }

When it is invoked, the searched ...

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.