ASP.NET Core Using ViewComponent
- ASP.NET Core ViewComponent
- 16373
|
In a large number of pages call the same view results, we can encapsulate the same content into a view component, of course, you can also pass parameters to the view component to return the response results, scenarios: leaderboards, forms, tables, etc., in this article, using asp.net core 3.1 to explain how to use ViewComponent ViewComponent tutorial.
View components
View components are similar to partial views, but more powerful. View components do not use model binding, but only rely on the data provided when the model is called. This article was written using controllers and views, but view components can also be used with Razor Pages.
View Component:
- Renders a block instead of the entire response.
- Includes the same separation of concerns and testability benefits found between controllers and views.
- Can have parameters and business logic.
- Typically called from a layout page.
View components can be used anywhere you have reusable rendering logic that is too complex for localized views, for example:
- Dynamic navigation menus
- Tag cloud (where the database is queried)
- Login panel
- Shopping cart
- Recent posts
- Sidebar content on a typical blog
- The login panel will be displayed on every page with a link to log out or log in, depending on the user's login status
Localized view
asp.net mvc loading dynamic data using RenderAction local view h ttps:// www.itsvse.com/thread-4828-1-1.html In asp.net mvc 5 we can use localized view with the following code:
Invocation method:
A ViewComponent is a replacement for a partial view.
Getting Started with ViewComponent
View component class:
- Fully supports constructor dependency injection
- Does not participate in the controller's lifecycle, which means you can't use filters in the view component
- Without further ado, let's get right to it, we're going to encapsulate the functionality of a component that categorizes a city area.
First of all, create a new "ViewComponents" folder under the project directory and create a new "RootClassification.cs" class file with the following code:
View page, we create a new "Components" folder under the project "/Views/Shared" folder, followed by a new " RootClassification" folder (here is and our new component class to object up), 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 is related to the runtime mechanism of asp.net core components.
The runtime searches for views 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 a view component is Default, which means that your view file will usually be named Default.cshtml. You can specify another view name when creating the view component result or calling the View method.
To call it, we just call it where we need to use the component, the code is as follows:
Re-run the project, the effect is as follows:
|
Rating
-
See all ratings
Previous:EF Core Series (3) Entity Framework Shadow Attributes [ 转载]Next:Website using swiper plugin to achieve picture slideshow (rotating chart)
|