in ASP. NET MVC 6, the View Components (VCs) feature is similar to a virtual view, but more powerful. VCs takes into account the advantages of views and controllers, and you can think of VCs as a mini controller. It is responsible for controlling a functional module in the application, such as:
Dynamic navigation Menu
Tag Cloud
Login panel
Shopping Cart
Recent Articles
Blog side Sidebar
If you create a login panel using VC, you can call it in many scenarios, such as:
You can get the data to render according to the user's needs. Add a VC to the page that needs the view control.
The VC contains two parts, a class (typically inherited from viewcomponent) and a razor view that calls methods in the VC class. Similar to the ASP. NET controller, VCs can be used as Poco, but more users tend to use methods and properties inherited from Vewcomponent.
VC is created in the following ways:
Inherit Viewcomponent.
Owns the [Viewcomponent] property, or a class derived from the owning [Viewcomponent] property.
Create a class that has the name viewcomponent as a suffix.
As with controllers, VCs must be public, non-nested, and non-abstract classes.
Add View Component Class
1. Create a folder named viewcomponents , and the View component class can be included under any folder in the project.
2. Create the PriorityListViewComponent.cs class under the viewcomponents folder.
3. Replace the original code of the PriorityListViewComponent.cs file with the following code:
using system.linq;using microsoft.aspnet.mvc;using todolist.models;namespace Todolist.viewcomponents{ public class prioritylistviewcomponent : viewcomponent { private readonly ApplicationDbContext db; public prioritylistviewcomponent (Applicationdbcontext context) { db = context; } Public iviewcomponentresult invoke (int maxpriority) { var items = db. Todoitems.where (x => x.isdone == false && x.priority <= maxpriority); return view (items); } }}
Code Comment:
· Because the prioritylistViewcomponent class inherits from the viewcomponent, the runtime references the class from view through the string "Prioritylist". Detailed elaboration will be made in subsequent chapters.
· The [Viewcomponent] property is used to set the alias that references the VC, for example, to create a class with the name XYZ, and we can set its reference alias with the following code:
[Viewcomponent (Name = "Prioritylist")]public class Xyz:viewcomponent
· The component uses a construction injector to make the data content effective, similar to the functionality of the Todo controller.
· Call the public method in view to pass any number of arguments. In the asynchronous version, InvokeAsync is available. In the subsequent chapters we will refer to the use of InvokeAsync and multiple parameters. In the previous code, the return value of the public method is the agent (Todoitems), with a priority of no less than maxpriority.
To add a view control
1. Create the Components folder under the views\todo folder, and note that this folder needs to be named components.
2. Create the prioritylist folder under the views\todo\components folder. The folder name must match the View component class name. Or the class name removes the suffix name (if you follow the convention when creating the class, use viewcomponent as the suffix). If the Viewcomponent property is used.
3. Create the default.cshtml Razor view under the views\todo\components\prioritylist folder and add the following markup:
@model ienumerable<todolist.models.todoitem>
The Razor view contains and displays the Todoitems. If the VC call method does not pass the name of the view (as shown in the example), then by default the view name is called for the method. In a subsequent article, you will explain how to pass the view name.
At the bottom of the views\todo\index.cshtml view, add a div with the call Prioritylistviewcomponent:
@model ienumerable<todolist.models.todoitem>
The tag @await Component.invokeasync () indicates that the syntax is used to invoke the VC. The first parameter is the name of the component we want to invoke. The remaining parameter parameters are passed to the VC. In this example, we pass "1" as the filter priority. The InvokeAsync method can contain any number of arguments.
The priority list is shown in slices:
@{ viewbag.title = "Todo page";} <div class= "Jumbotron" > 650) this.width=650; "width=" 432 "height=" 494 "title=" image "style=" border:0px;padding-top:0px;padding-right:0px; Padding-left:0px;background-image:none; "alt=" image "src=" http://images.cnitblog.com/blog/139239/201501/ 091043422503318.png "border=" 0 "/>
Note: VCs are usually added to the views\shared folder because it is not just a controller.
Adding InvokeAsync to Priority componentsUpdate the Prioritylistviewcomponent class with the following code:
using system.linq;using microsoft.aspnet.mvc;using todolist.models;using system.threading.tasks;namespace todolist.viewcomponents{ public class prioritylistviewcomponent : viewcomponent { private readonly ApplicationDbContext db; public prioritylistviewcomponent (Applicationdbcontext context) { db = context; } // Synchronous Invoke removed. public async Task<IViewComponentResult> InvokeAsync (Int maxpriority, bool isdone) { string MyView = "Default"; // if asking for all completed tasks, render with the "PVC" view. if (maxpriority > 3 && isdone == true) { MyView = "PVC"; } var Items = await getitemsasync (Maxpriority, isdone); Return view (Myview, items); } private task<iqueryable<todoitem>> getitemsasync (int Maxpriority, bool isdone) { return task.fromresult (GetItems (MaxPriority, isDone) ); } private iqueryable<todoitem> getitems (Int maxpriority, bool isdone) { var items = db. Todoitems.where (x => x.isdone == isdone && & NBsp; x.priority <= maxpriority); string msg = "priority <= " + Maxpriority.tostring () + " && isdone == " + isdone.tostring (); viewbag.prioritymessage = msg; return items; } }}
Note : This removes the Invoke method for synchronization and replaces it with a more powerful asynchronous method.
To modify the VC view display priority information:
@model ienumerable<todolist.models.todoitem>
Finally, update the views\todo\index.cshtml view File:
@* Markup removed for brevity. *@ <div class= "col-md-4" > @await component.invokeasync ("Prioritylist", 2, True) </div></di V>
The Prioritylistviewcomponent class and the index view are displayed in slices to modify the effect:
650) this.width=650; "Width=" 416 "height=" 526 "title=" image "style=" border:0px;padding-top:0px;padding-right:0px; Padding-left:0px;background-image:none; "alt=" image "src=" http://images.cnitblog.com/blog/139239/201501/ 091043437341532.png "border=" 0 "/>
Specify the view nameSome complex VCs may need to specify a particular view in some cases, the following code is the method of specifying the view through the InvokeAsync method:
Public async task<iviewcomponentresult> InvokeAsync (int maxpriority, bool isDone) {string MyView = "Default"; If asking for all completed tasks, render with the "PVC" view. if (maxpriority > 3 && isDone = = True) {MyView = "PVC"; } var items = await Getitemsasync (maxpriority, IsDone); Return View (MyView, items);}
Change views\todo\components\prioritylist\default.cshtml to views\todo\components\prioritylist\pvc.cshtml view. Change the PVC view control to verify its use:
@model ienumerable<todolist.models.todoitem>
Finally, the views\todo\index.cshtml file needs to be updated:
Refresh the page to see the effect of the change.
In MVC6, when you change the controller (or any other code), you do not need to recompile or rerun the app, just save the Code and refresh the page.
These are the view components that we would like to share with you today, and in the next article we will cover the following two parts:
Please look forward to it.
Original link: HTTP://WWW.ASP.NET/VNEXT/OVERVIEW/ASPNET-VNEXT/VC
This article is from the "Grape City Control Technology Team Blog" blog, be sure to keep this source http://powertoolsteam.blog.51cto.com/2369428/1600969
ASP 5 Series Tutorial (iii): View Components Introduction