Introduction
In our web applications, we often need to display a custom drop-down for user selection. In this article, we will focus on creating a custom drop-down list in Blazor WASM.
The most interesting part is that this custom drop-down is a Blazor component which will have its own UI and backend logic.
Once created, this can be easily used across applications in any number of razor pages, thus saving effort and allowing for maximum reusability.
Prerequisites
This article assumes you have a basic working knowledge of Blazor web assembly and .NET Core.
I will be using the API and Repository layer of my previous Blazor projects.
Kindly refer to the below articles to understand UI binding, database, and API interaction in Blazor apps.
Through this article, we will cover the following topics.
- How to create a custom drop-down component in Blazor?
- How to use a custom drop-down component on any razor pages?
- How to call the Data service in the Blazor app and get data using the Entity Framework core required for component binding?
Output
Region drop-down component
![All employee]()
Implementation
Before implementation, you need to know the prerequisite.
- We need to create a new Blazor web assembly project with .NET Core hosted.
- The basic project structure is shown below.
![Edit razor]()
- EmployeePortal.The client is Blazor Web Assembly Project
- EmployeePortal.The server is a .NET Core 3.1 Project Where our API and Database Repository would reside
- EmployeePoral.Shared is the .NET Standard Library Project which would hold the required Models for our project
Creating the dropdown component
Create a new Model class Region in the shared project as below.
In the components folder of the client project add a new Razor and class file. The detailed structure of the client folder is mentioned below.
![Component folder]()
Add the below code in Regions.razor
It is very important to check if Regionsmaster is null else it will give an object reference error.
Add the below code in Regions.cs
- This is our component class which would invoke EmployeeDataservice
- Which would in turn call .NET Core API using System.
When the component is getting initialized we are getting data for all the regions.
This Register Data service is already a registered program.cs of a client project.
On drop-down value change, the OnValueChanged change will be fired and the event args will have its latest value.
Data Service would have the GetRegions function as below which would call the .NET Core API.
In the .NET Core Project create a new API to retrieve Region data.
The Employee Repository needs to be registered in startup.cs file of the .NET Core project.
Repository Function to get Regions data from SQL database.
The Regions dbset needs to be defined in the Entity framework db context class.
We have now completed with API repository and UI for our dropdown component.
Using our custom-created component on the home page
We can use our Regions component on AddEmployeeDialog.razor page.
The selected region is again bound back in the RegionId property of the AddEmployeeDialog class
Thus we get the selected RegionId which can be saved into the database along with Employee Data.
Summary
Through this article, we have learned how to create a Custom Dropdown Blazor component model and use it in our razor pages. We also learned how the selected value is set in the page class property.
Thanks a lot for reading. I hope you liked this article. Please share your valuable suggestions and feedback. Write in the comment box in case you have any questions. Have a good day!