架构师_程序员_码农网

Retrieve password
Register

QQ登录

Just one step to get started

Search
Views:20585|Replies: 2
打印 上一主题 下一主题

[ASP.NET]ASP.NET Core Using ViewComponent View Component Tutorial

[Copy link]
ASP.NET Core Using ViewComponent
跳转到指定楼层
owner
发表于 2021-2-7 16:18:25| 看该作者回帖奖励|ReverseBrowse|Read Mode
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

Participants 1 MB +1 Contribution +1 Put awayReason
fessicon + 1 + 1 Support the owner with a good post and I'll do the same!....

See all ratings





Previous:EF Core Series (3) Entity Framework Shadow Attributes [ 转载]
Next:Website using swiper plugin to achieve picture slideshow (rotating chart)
NET, only published in the practice process, encountered technical difficulties, do not mislead others.
fessicon
发表于 2021-8-12 10:21:01| 看该作者
Reply in order to see the code?
Code farmer network, only published in the process of practice, encountered technical difficulties, do not mislead others.
ugg boots sale
uggs outlet online
楼主 | 发表于 2023-8-13 17:55:05| 看该作者
I'm not sure if this is the best way to get the most out of ASP.NET Core, but I'm sure it's the best way to get the most out of ASP.NET Core.
You need to log in before you can post back Log in | Register

This version of the integral rules

DISCLAIMER:
All software, programming materials or articles published by Code Farmer are only used for learning and research purposes; the above content shall not be used for commercial or illegal purposes, otherwise, all the consequences should be borne by the user. This site information from the network, copyright dispute has nothing to do with this site. You must completely remove the above content from your computer within 24 hours of downloading. If you like the program, please support the genuine software, buy registration and get better genuine service. If there is any infringement, please contact us by email to deal with it.

Mail To:help@itsvse.com

QQ| ( ICP备14021824号-2 )|Sitemap

GMT+8, 2024-9-19 00:41

Quick ReplyBack to topBack to list