ASP.NET Monsters #24: View Components in ASP.NET MVC Core

Sign in to queue

Description

If you need a way to bundle up bits of UI and related behind-the-scenes logic, chances are you're looking for View Components in ASP.NET MVC. This feature of the MVC Framework lets you build reusable components in a new way that better participates in the HTTP request and controller lifecycle.

In previous versions of the MVC Framework, we had child actions, but those have gone the way of the dodo. In this video, Monster Dave walks us through leveraging View Components - including a brief cross over into dependency injection - in our Razor views.

Embed

Download

Download this episode

The Discussion

  • User profile image
    scyonx

    Every time i see someone talk about view components I feel like the concept should be merged with taghelpers to some extent. They both can have services register in the DI container injected into them, they bot do some processing and output markup to the page. 

    The way things are today, what are the advantages of using TagHelpers in one instance or ViewComponents in another

     

     

  • User profile image
    David​Paquette

    @scyonx: Excellent question.

    One advantage of View Components is that you can define the View portion in Razor. With TagHelpers you are limited to generating HTML using C#.  

    An advantage of Tag Helpers is that the markup when using the tag helper is much cleaner (<my-tag-helper>) vs @Component.Invoke. As I showed towards the end of the video I do think this will change for view components eventually.

    TagHelpers also have a different use case given that you can target existing HTML elements. You can't do that with ViewComponents. 

Add Your 2 Cents