ASP. NET Core Learning Notes (Viewcomponent components)

Source: Internet
Author: User

Refer:https://docs.microsoft.com/en-us/aspnet/core/mvc/views/view-components

The component of core and Angular are different.

The component of core is just an upgrade to partial view.

The former partial view is just a view package, and component is the Controller + view package

So convenient for some.

First define a class inheritance Viewcomponent, component's name is class name, of course, we can change it,

[Area ("Web")][viewcomponent (Name="keatkeat")]//Modify name Public classmyheader:viewcomponent{PrivateDB DB {Get;Set; }  PublicMyHeader (db db) { This. db =DB; }     PublicTask<iviewcomponentresult> InvokeAsync (stringvalue)//can be passed at the time of invocation Oh {varVM =NewViewModel {Name=value}; returnTask.fromresult<iviewcomponentresult>(View (VM)); }}

Call

@addTagHelper *, Project

@{var value = "keatkeat87";} @await Component.invokeasync ("Keatkeat", value) @await Component.invokeasync (nameof ( Project.Web.ViewComponents.MyHeader.MyHeader), value)
<value= "@value" ></vc:keatkeat>

There are 3 ways to call it.

The first is to write component names.

The second is to find the class through nameof, if you change the name, then the call will be broken.

The third is through the element mode (need to add @addTagHelper in _viewimports.cshtml *, Project, project is the name of my project, directly put the entire project assembly on it)

The viewcomponent template differs from the general controller lookup, and the general controller View name is index.cshtml

and Viewcomponent is components/componentname/default.cshtml.

ASP. NET Core Learning Notes (Viewcomponent components)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.