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.
- https://www.c-sharpcorner.com/article/blazor-web-assembly-3-2-addeditdelete-full-functional-application-part-1-crud/
- https://www.c-sharpcorner.com/article/blazor-web-assembly-3-2-addeditdelete-fully-functional-application-part-2/
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

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.

- 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.
using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace EmployeePortal.Shared {
[Table("RegionMaster")]
public class Region {
public int RegionId {
get;
set;
}
public string RegionName {
get;
set;
}
}
}
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.

Add the below code in Regions.razor
@if (Regionmaster == null)
{
<p>
<em>Loading ...</em>
</p>
}
else
{
<select class="custom-select" @onchange="OnValueChanged" title="Region is required">
<option value="Select" selected disabled="disabled">(Choose Region)</option>
@foreach (var region in Regionmaster)
{
<option value="@region.RegionId">@region.RegionName</option>
}
</select>
}
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.
public partial class Regions : ComponentBase { [Parameter] public string RegionId { get; set; } public IEnumerable<Region> Regionmaster { get; set; } [Inject] public IEmployeeDataService EmployeeDataService { get; set; } [Parameter] public string Value { get; set; } [Parameter] public EventCallback<string> ValueChanged { get; set; } protected async override Task OnInitializedAsync() { Regionmaster = (await EmployeeDataService.GetRegions()).ToList(); } private Task OnValueChanged(ChangeEventArgs e) { Value = e.Value.ToString(); return ValueChanged.InvokeAsync(Value); } }
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.
builder.Services.AddHttpClient<IEmployeeDataService, EmployeeDataService>(
client => client.BaseAddress = new Uri(builder.HostEnvironment.BaseAddress)
);
On drop-down value change, the OnValueChanged change will be fired and the event args will have its latest value.

Join the conversation! Your thoughts help the community grow.