View Component in ASP.NET Core

View Component is one of the features in ASP.NET Core MVC. If you work with ASP.NET Web Forms, you might be very familiar with user controls. View Components are exactly working the same way how user control works.

ASP.NET Core Introduced View Components to allow the developers to create reusable UI Components that can be rendered in any View or shared to multiple Views, which helps in reduce the amount of repeated duplicate code and making it easier to maintain.

ASP.NET Core View Component

View Component have two parts,
  • Component: Is a Class that derives from ViewComponent class with a single method called InvokeAsync
  • View: Is a Razor Cshtml file with the outcome result UI of the component. All the data binding and look and feel of the component goes here. 

Create View Component

Here we are going to create a sample View Component called “Left Menu”.

Let’s assume that you need to create set of menu links of an application in certain pages only at left side. Instead of adding those links manually on each page, let’s create a component called “Left Menu” and reuse it in multiple pages wherever you need.
For the above scenario, we’ll start building the View Component.
 

Step 1 (Create Component)

Firstly, Create an ASP.NET Core MVC application.

Create a folder called “Components” to place all custom View Components.

Create a new View Component called “LeftMenuViewComponent.cs” file under the Components folder.

View Component class in ASP.NET Core

The class LeftMenuViewComponent is inherited from ViewComponent class with a method InvokeAsync as shown below:

[pre class="brush:csharp; toolbar: false;" title=""] public class LeftMenuViewComponent : ViewComponent { public async Task<IViewComponentResult> InvokeAsync(bool isSuperUser) { List<string> menuItems = new List<string>() { "Home", "Profile", "About", "Contact" }; if (isSuperUser) menuItems.Add("Administration"); return View(menuItems); } } [/pre]
In the above InvokeAsync method we are creating a list of menu items required and pass them to component UI.

That’s it. Isn’t that simple to create Component class? Now, let’s look into creating the Component UI to build the “Left Menu” using menu items coming from Component class.

Step 2 (Create View)

Since we are creating the reusable component and shared to across the many views, we will create the Component in a Shared folder.

Under the Shared folder, create a subfolder called Components. Again, create another subfolder under Components folder called LeftMenu. Create a new Razor view called Default.cshtml under LeftMenu folder as shown below:

View Component UI in ASP.NET Core

Now build the “Left Menu” using the Component data. Here I am using the Bootstrap to build the “Left Menu”, you can create the Menu UI on your own with different look.

Here is the HTML code to build the “Left Menu”.

[pre class="brush:html; toolbar: false;" title=""] <div id="wrapper"> <div id="sidebar-wrapper"> <ul class="sidebar-nav"> <li class="sidebar-brand"> <a href="#"> <b>Left Menu</b> </a> </li> @foreach (string menuItem in Model) { <li> <a href="#">@menuItem</a> </li> } </ul> </div> </div> [/pre]
And, here are the CSS styles need for look for “Left Menu”.

[pre class="brush:css; toolbar: false;" title=""] <style type="text/css"> #sidebar-wrapper { z-index: 1000; position: fixed; left: 250px; width: 0; margin-left: -250px; overflow-y: auto; background: #000; } .sidebar-nav li a { display: block; text-decoration: none; color: #999999; } .sidebar-nav li a:hover { text-decoration: none; color: #fff; background: rgba(255,255,255,0.2); } #sidebar-wrapper { width: 150px; height: 300px; } </style> [/pre]
The view is just a simple HTML unordered list that iterates over the menu items passed from the LeftMenuViewComponent View Component. With this, we are done with building the Component UI.

Step 3 (Consume View Component)

Now let’s start consuming the newly built View Component in any View or Layout.

Let’s go to one of the sample View pages and call the below line to render the newly View Component, in this case I have called the below code in Index.cshtml of Home controller.

[pre class="brush:csharp; toolbar: false;" title=""] @await Component.InvokeAsync("LeftMenu", true) [/pre]
The above line of code calls the InvokeAsync method of LeftMenuViewComponent class, pass the menu items to Razor view and render the View in the place where you called above code of line.

Once you run the application and go to the View where you called, you should see View Component rendered as below:

Create View Component in ASP.NET Core
Same way, now you can call the above line of code in any View or Layout wherever you need. 

The View Component we build also using the parameter, which is passed from the View where it hosted the View Component, and based on the parameter value our menu items will change as per the Component class logic.

Hope this helps you to understand the View Component in ASP.NET Core, creating and rendering in Views or Layout.

No comments:

Powered by Blogger.