From the course: Learning ASP.NET Core MVC

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Enhancing a view component with logic and dynamic markup

Enhancing a view component with logic and dynamic markup

From the course: Learning ASP.NET Core MVC

Start my 1-month free trial

Enhancing a view component with logic and dynamic markup

- [Instructor] In the previous video, I showed you how to create and render a basic view component that rendered some basic string content. But now that those basics are out of the way, let's spice it up a little. As I built the view component class, I called out how it resembles a controller class and that's exactly how you should think of it, like a mini controller action, executing whenever you reference the view component. That means a few things, first you'll probably want to return a view from the invoke action, rather than static content. You do that by using the view helper method on the view component base class, and just like in the controller action, when you do this you'll need to change the return type to an action result type rather than a simple string. An IView component result type to be exact. The other great thing that view component classes share with controllers, is that you are able to inject application logic using dependency injection. For instance, in this…

Contents