The DevExpress WPF Subscription was built with you in mind. It was built for those who demand the highest quality and expect the best performance.for those who require reliable tools engineered for today and tomorrow. Our WPF Subscription includes a powerhouse collection of Office-inspired user interface components.
By Rick Anderson
View or download sample code (how to download)
View components are similar to partial views, but they're much more powerful. View components don't use model binding, and only depend on the data provided when calling into it. This article was written using controllers and views, but view components also work with Razor Pages.
A view component:
View components are intended anywhere you have reusable rendering logic that's too complex for a partial view, such as:
A view component consists of two parts: the class (typically derived from ViewComponent) and the result it returns (typically a view). Like controllers, a view component can be a POCO, but most developers will want to take advantage of the methods and properties available by deriving from ViewComponent
.
When considering if view components meet an app's specifications, consider using Razor Components instead. Razor Components also combine markup with C# code to produce reusable UI units. Razor Components are designed for developer productivity when providing client-side UI logic and composition. For more information, see Create and use ASP.NET Core Razor components.
This section contains the high-level requirements to create a view component. Later in the article, we'll examine each step in detail and create a view component.
A view component class can be created by any of the following:
[ViewComponent]
attribute, or deriving from a class with the [ViewComponent]
attributeLike controllers, view components must be public, non-nested, and non-abstract classes. The view component name is the class name with the 'ViewComponent' suffix removed. It can also be explicitly specified using the ViewComponentAttribute.Name
property.
A view component class:
Fully supports constructor dependency injection
/kontrol-x1-traktor-pro-2-mapping.html. TRAKTOR KONTROL X1 is the compact DJ performance controller with responsive controls over two TRAKTOR PRO 3 decks and FX units. COMPACT TRAKTOR CONTROL Whether DJing all-digital or with timecode control, the mighty TRAKTOR KONTROL X1 gives you total control of decks and effects in TRAKTOR PRO 3, with a unique combination of power and portability.
Doesn't take part in the controller lifecycle, which means you can't use filters in a view component
A view component defines its logic in an InvokeAsync
method that returns a Task<IViewComponentResult>
or in a synchronous Invoke
method that returns an IViewComponentResult
. Parameters come directly from invocation of the view component, not from model binding. A view component never directly handles a request. Typically, a view component initializes a model and passes it to a view by calling the View
method. In summary, view component methods:
InvokeAsync
method that returns a Task<IViewComponentResult>
or a synchronous Invoke
method that returns an IViewComponentResult
.ViewComponent
View
method.The runtime searches for the view in the following paths:
The search path applies to projects using controllers + views and Razor Pages.
The default view name for a view component is Default, which means your view file will typically be named Default.cshtml. You can specify a different view name when creating the view component result or when calling the View
method.
We recommend you name the view file Default.cshtml and use the Views/Shared/Components/{View Component Name}/{View Name} path. The PriorityList
view component used in this sample uses Views/Shared/Components/PriorityList/Default.cshtml for the view component view.
To customize the view search path, modify Razor's ViewLocationFormats collection. For example, to search for views within the path '/Components/{View Component Name}/{View Name}', add a new item to the collection:
In the preceding code, the placeholder '{0}' represents the path 'Components/{View Component Name}/{View Name}'.
To use the view component, call the following inside a view:
The parameters will be passed to the InvokeAsync
method. The PriorityList
view component developed in the article is invoked from the Views/ToDo/Index.cshtml view file. In the following, the InvokeAsync
method is called with two parameters:
For ASP.NET Core 1.1 and higher, you can invoke a view component as a Tag Helper:
Pascal-cased class and method parameters for Tag Helpers are translated into their kebab case. The Tag Helper to invoke a view component uses the <vc></vc>
element. The view component is specified as follows:
To use a view component as a Tag Helper, register the assembly containing the view component using the @addTagHelper
Studiolinked trophies vst crack. directive. If your view component is in an assembly called MyWebApp
, add the following directive to the _ViewImports.cshtml file:
You can register a view component as a Tag Helper to any file that references the view component. See Managing Tag Helper Scope for more information on how to register Tag Helpers.
The InvokeAsync
method used in this tutorial:
In Tag Helper markup:
In the sample above, the PriorityList
view component becomes priority-list
. The parameters to the view component are passed as attributes in kebab case.
View components are typically invoked from a view, but you can invoke them directly from a controller method. While view components don't define endpoints like controllers, you can easily implement a controller action that returns the content of a ViewComponentResult
.
In this example, the view component is called directly from the controller:
Download, build and test the starter code. It's a simple project with a ToDo
controller that displays a list of ToDo items.
Create a ViewComponents folder and add the following PriorityListViewComponent
class:
Notes on the code:
View component classes can be contained in any folder in the project.
Because the class name PriorityListViewComponent ends with the suffix ViewComponent, the runtime will use the string 'PriorityList' when referencing the class component from a view. I'll explain that in more detail later.
The [ViewComponent]
attribute can change the name used to reference a view component. For example, we could've named the class XYZ
and applied the ViewComponent
attribute:
The [ViewComponent]
attribute above tells the view component selector to use the name PriorityList
when looking for the views associated with the component, and to use the string 'PriorityList' when referencing the class component from a view. I'll explain that in more detail later.
The component uses dependency injection to make the data context available.
InvokeAsync
exposes a method which can be called from a view, and it can take an arbitrary number of arguments.
The InvokeAsync
method returns the set of ToDo
items that satisfy the isDone
and maxPriority
parameters.
Create the Views/Shared/Components folder. This folder must be named Components.
Create the Views/Shared/Components/PriorityList folder. This folder name must match the name of the view component class, or the name of the class minus the suffix (if we followed convention and used the ViewComponent suffix in the class name). If you used the ViewComponent
attribute, the class name would need to match the attribute designation.
Create a Views/Shared/Components/PriorityList/Default.cshtml Razor view:
The Razor view takes a list of TodoItem
and displays them. If the view component InvokeAsync
method doesn't pass the name of the view (as in our sample), Default is used for the view name by convention. Later in the tutorial, I'll show you how to pass the name of the view. To override the default styling for a specific controller, add a view to the controller-specific view folder (for example Views/ToDo/Components/PriorityList/Default.cshtml).
If the view component is controller-specific, you can add it to the controller-specific folder (Views/ToDo/Components/PriorityList/Default.cshtml).
Add a div
containing a call to the priority list component to the bottom of the Views/ToDo/index.cshtml file:
The markup @await Component.InvokeAsync
shows the syntax for calling view components. The first argument is the name of the component we want to invoke or call. Subsequent parameters are passed to the component. InvokeAsync
can take an arbitrary number of arguments.
Test the app. The following image shows the ToDo list and the priority items:
You can also call the view component directly from the controller:
A complex view component might need to specify a non-default view under some conditions. The following code shows how to specify the 'PVC' view from the InvokeAsync
method. Update the InvokeAsync
method in the PriorityListViewComponent
class.
Copy the Views/Shared/Components/PriorityList/Default.cshtml file to a view named Views/Shared/Components/PriorityList/PVC.cshtml. Add a heading to indicate the PVC view is being used.
Update Views/ToDo/Index.cshtml:
Run the app and verify PVC view.
If the PVC view isn't rendered, verify you are calling the view component with a priority of 4 or higher.
Change the priority parameter to three or less so the priority view isn't returned.
Temporarily rename the Views/ToDo/Components/PriorityList/Default.cshtml to 1Default.cshtml.
Test the app, you'll get the following error:
Copy Views/ToDo/Components/PriorityList/1Default.cshtml to Views/Shared/Components/PriorityList/Default.cshtml.
Add some markup to the Shared ToDo view component view to indicate the view is from the Shared folder.
Test the Shared component view.
If you want compile time safety, you can replace the hard-coded view component name with the class name. Create the view component without the 'ViewComponent' suffix:
Add a using
statement to your Razor view file, and use the nameof
operator:
The framework handles invoking a synchronous Invoke
method if you don't need to perform asynchronous work. The following method creates a synchronous Invoke
view component:
The view component's Razor file lists the strings passed to the Invoke
method (Views/Home/Components/PriorityList/Default.cshtml):
The view component is invoked in a Razor file (for example, Views/Home/Index.cshtml) using one of the following approaches:
To use the IViewComponentHelper approach, call Component.InvokeAsync
:
The view component is invoked in a Razor file (for example, Views/Home/Index.cshtml) with IViewComponentHelper.
Call Component.InvokeAsync
:
To use the Tag Helper, register the assembly containing the View Component using the @addTagHelper
directive (the view component is in an assembly called MyWebApp
):
Use the view component Tag Helper in the Razor markup file:
The method signature of PriorityList.Invoke
is synchronous, but Razor finds and calls the method with Component.InvokeAsync
in the markup file.
Each parameter in a view component is a required attribute. See this GitHub issue. If any parameter is omitted:
InvokeAsync
method signature won't match, therefore the method won't execute.This article demonstrates how to create and deploy code components using Power Apps CLI. Ensure that you have installed Microsoft Power Apps CLI.
To begin, open Developer Command Prompt for VS 2017 after installing Power Apps CLI.
In the Developer Command Prompt for VS 2017, create a new folder on your local machine, for example, C:Usersyour nameDocumentsMy_code_Component using the command mkdir <Specify the folder name>
.
Go to the newly created folder using the command cd <specify your new folder path>
.
Create a new component project by passing some basic parameters using the command:
Note
Currently, Power Apps CLI supports two types of components: field and dataset for model-driven apps. For canvas apps, only the field type is supported for this experimental preview.
To retrieve all the required project dependencies, run the command npm install
.
Open your project folder C:Users<your name>Documents<My_code_Component>
in any developer environment of your choice and get started with your code component development. The quickest way to get started is by running code .
from your command prompt once you are in the C:Users<your name>Documents<My_code_Component>
directory. This command opens your component project in Visual Studio Code.
Implement the required artifacts for the component like manifest, component logic, and styling and then build the component project. More information: Create your first code component
To build the component project, open the project folder that contains package.json
in Visual Studio Code and use the (Ctrl-Shift-B) command, then select the build options.
Alternatively, you can build the component quickly using the npm run build
command in the Developer Command Prompt for VS 2017 window.
Tip
To debug the component during or after the build operation, see Debug a code component.
Finally when you're done implementing the component logic in TypeScript, you need to bundle all the code component elements into a solution file so that you can import the solution into Common Data Service. More information: Package a code component
Debug code components
Package a code component
Add code components to a field or entity
Updating existing code components
Power Apps component framework API reference
Power Apps component framework overview