- GemBox.Pdf is a.NET component that enables developers to read, merge and split PDF files or execute low-level object manipulations from.NET applications in a simple and efficient way. If you want to create complex PDF documents, use GemBox.Document, GemBox.Spreadsheet, and GemBox.Presentation, which all have PDF exporting capability.
- PDFKit.NET 5.0 is compatible with.NET Core. You can now run your PDF app on Windows, Linux and MacOS. PDFKit.NET 5.0 is compatible with Xamarin.
Free Spire.PDF for.NET is a Community Edition of the Spire.PDF for.NET, which is a totally free PDF API for commercial and personal use.As a standalone.NET library, Free Spire.PDF for.NET enables developers to create, write, edit, convert, print, handle and read PDF files on any.NET( C#, VB.NET, ASP.NET,.NET Core) applications.
-->By Rick Anderson
View or download sample code (how to download)
View components
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:
- Renders a chunk rather than a whole response.
- Includes the same separation-of-concerns and testability benefits found between a controller and view.
- Can have parameters and business logic.
- Is typically invoked from a layout page.
View components are intended anywhere you have reusable rendering logic that's too complex for a partial view, such as:
- Dynamic navigation menus
- Tag cloud (where it queries the database)
- Login panel
- Shopping cart
- Recently published articles
- Sidebar content on a typical blog
- A login panel that would be rendered on every page and show either the links to log out or log in, depending on the log in state of the user
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.
Creating a view component
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.
The view component class
A view component class can be created by any of the following:
- Deriving from ViewComponent
- Decorating a class with the
[ViewComponent]
attribute, or deriving from a class with the[ViewComponent]
attribute - Creating a class where the name ends with the suffix ViewComponent
Like 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
- Doesn't take part in the controller lifecycle, which means you can't use filters in a view component
View component methods
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:- Define an
InvokeAsync
method that returns aTask<IViewComponentResult>
or a synchronousInvoke
method that returns anIViewComponentResult
. - Typically initializes a model and passes it to a view by calling the
ViewComponent
View
method. - Parameters come from the calling method, not HTTP. There's no model binding.
- Are not reachable directly as an HTTP endpoint. They're invoked from your code (usually in a view). A view component never handles a request.
- Are overloaded on the signature rather than any details from the current HTTP request.
View search path
The runtime searches for the view in the following paths:
- /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 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.Customize the view search path
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}'.
Invoking a view component
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:Invoking a view component as a Tag Helper
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
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.Invoking a view component directly from a controller
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:
Walkthrough: Creating a simple view component
Download, build and test the starter code. It's a simple project with a
ToDo
controller that displays a list of ToDo items.Add a ViewComponent class
![Net Net](https://www.edrawsoft.com/images/pdf/addpdfcomform.png)
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 classXYZ
and applied theViewComponent
attribute: - The
[ViewComponent]
attribute above tells the view component selector to use the namePriorityList
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 ofToDo
items that satisfy theisDone
andmaxPriority
parameters.
Create the view component Razor view
- 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 componentInvokeAsync
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. Eldredge sewing machine serial numbers.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:
Specifying a view name
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:
.net Pdf Component Apitron
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.
Examine the view path
- 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.
Avoiding hard-coded strings
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:Perform synchronous work
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.All view component parameters are required
Each parameter in a view component is a required attribute. See this GitHub issue. If any parameter is omitted:
- The
InvokeAsync
method signature won't match, therefore the method won't execute. - The ViewComponent won't render any markup.
- No errors will be thrown.
Additional resources
Current Statistics
Electronic Components | 900 Billion+ |
Line Items | 50 Million+ |
Page Views / Month | 3,500,000+ |
Suppliers | 3,000+ |
For Component Manufacturers
netCOMPONENTS offers a custom designed solution exclusively for component manufacturers called the Distributor Inventory Locator Program (DILPsm). netCOMPONENTS has been designing and implementing custom DILPsm solutions for the electronics industry since 1997 and is considered a worldwide leader in this type of outsourced service.
For more information on DILPsm, including a detailed description, pricing, and demo, please contact a netCOMPONENTS representative at [email protected].
Advertising
netCOMPONENTS offers banner advertising programs on sites that are published by netCOMPONENTS targeting electronic component buyers, supply chain managers, procurement professionals and electronic engineers. For more information on availability and pricing, please contact us at [email protected].
.net Pdf Component
netCOMPONENTS Escrow
Additional information on netCOMPONENTS Escrow is available to registered Member and Supplier users by clicking on the 'Escrow' menu option after login.
netCOMPONENTS is a market leader in sourcing services for the worldwide electronic components industry. netCOMPONENTS is the world's premier destination for the sourcing and procurement of electronic components, connecting members (buyers) and suppliers (sellers) in a direct and vendor-neutral environment!
Membership includes:
- Part Searches
- Unlimited locations and users
- Search by Worldwide Region
- Search results differentiation of authorized suppliers
- Search results differentiation of in-stock inventory
- Search results access to manufacturer data sheets on many line items
- Search results customizable settings and filters
- Search results supplier quality indicators
- Supplier Quality Rating System (SQRS) to rate, and view ratings on, suppliers
- Shopping cart link to select suppliers
- Multi-Lingual Messaging System to send RFQs and POs to suppliers
- Part Alerts to be notified when a part is added to netCOMPONENTS database
- netCOMPONENTS Escrow (additional fees apply)
- many more additional features & functionality..
Member (Buyer) Services Features
Membership for purchasing agents, buyers and procurement professionals enables part searches on the netCOMPONENTS database and access to an array of powerful sourcing features.
Register as a Member.net Pdf Component
Uploading Inventory includes:
- List inventory
- Unlimited locations and users
- Receive qualified/targeted RFQs and POs
- Multi-Lingual Messaging System to receive RFQs and POs from members
- netCOMPONENTS Escrow (additional fees apply)
- many more additional features & functionality..
Supplier (Seller) Services Features
Stocking distributors and other companies with inventory for sale are able to register and list their inventory.