This article is a mirror article of machine translation, please click here to jump to the original article.

View: 35979|Reply: 2

[ASP.NET] ASP.NET Core ViewComponent Tutorial on Using ViewComponent

[Copy link]
Posted on 2021-2-7 16:18:25 | | | |
When a large number of pages call the same view result, we can encapsulate the same content into a view component, of course, we can also pass parameters to the view component and return the response result, scenarios: leaderboards, forms, tables, etc., this article uses asp.net core 3.1 to explain how to use the ViewComponent view component tutorial.

View components

The View component is similar to a local view, but more powerful. The view component does not use model bindings, but only relies on the data provided when the model is called. This article was written using controllers and views, but the view component can also be used with Razor Pages.

View Components:

  • Render blocks instead of the entire response.
  • Includes the same separation of concerns and testability benefits found between controllers and views.
  • It can have parameters and business logic.
  • Usually called from the layout page.


View components can be used anywhere you have reusable rendering logic that is too complex for a local view, such as:

  • Dynamic navigation menu
  • Tag cloud (where the database is queried)
  • Login panel
  • Shopping cart
  • Recently published articles
  • Sidebar content on a typical blog
  • The login panel will appear on each page with a link to sign out or log in, depending on the user's login status


Partial view

asp.net mvc uses the RenderAction local view to load dynamic data
https://www.itsvse.com/thread-4828-1-1.html

In asp.net MVC 5 we can use a local view with the following code:



Call method:



ViewComponent is an alternative to local views.

ViewComponent to get started

View component class:

  • Constructor dependency injection is fully supported
  • Not involved in the controller's lifecycle, which means you can't use filters in view components
  • Without further ado, let's get straight to the point where we want to encapsulate the component functionality of an urban area classification.


First, create a new "ViewComponents" folder under the project directory and create a new "RootClassification.cs" class file with the following code:



On the view page, we create a new "Components" folder under the "/Views/Shared" folder of the project, then create a new "RootClassification" folder (this is the object with the new component class we created), and then create a new "Default.cshtml" file, the code is as follows:



The structure is as follows:



The detailed path is: /Views/Shared/Components/RootClassification/Default.cshtml

Why build such a path? This has to do with how the asp.net Core component works.

The runtime searches for the view in the following path:

/Views/{Controller Name}/Components/{View Component Name}/{View Name}
/Views/Shared/Components/{View Component Name}/{View Name}
/Pages/Shared/Components/{View Component Name}/{View Name}

The default view name for the view component is Default, which means that your view file will usually be named Default.cshtml. When creating a view component result or calling a View method, you can specify a different view name.

Calling, we can call where we need to use the component, the code is as follows:



Rerun the project with the following rendering:

Score

Number of participants1MB+1 contribute+1 Collapse reason
fessicon + 1 + 1 Support the owner to post a good post, and I will also post a good post!.

See all ratings





Previous:EF Core Series (3) Entity Frame Shadow Attributes [Reprint]
Next:Website uses the swiper plugin to implement an image slideshow (carousel)
Posted on 2021-8-12 10:21:01 |
Reply to see the code?
 Landlord| Posted on 2023-8-13 17:55:05 |
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com