When the Edit Employee form loads, we want to retrieve the list of all departments from the database and bind them to the Department dropdownlist. In addition to binding the list of all departments, we also want the employee's department to be selected.
For example, if the employee's department is Payroll, in addition to displaying the list of all departments, we want the Payroll department to be selected.

Blazor input select example
Code explanation
- We use the
InputSelectcomponent, to render an htmlselectelement Departmentsproperty in the component class carries the list of all departments to the view.- The
foreachloop, loops through the list and creates aselectelement option for each department. - The value for the option is the department id and the display text is the department name.
- If the employee belongs to IT department, it should be selected. For this we use
@bind-Valueattribute. This attribute provides two-way data-binding i.e on the initial page load, the employee department is selected and if we change the selection, the newly selected department value is automatically passed to theDepartmentIdproperty in the component class. - Binding the select element to an integer is not supported and throws the following exception.
Microsoft.AspNetCore.Components.Forms.InputSelect`1[System.Int32] does not support the type 'System.Int32'
Edit Employee Component View (EditEmployee.razor)
Edit Employee Component Class (EditEmployeeBase.cs)
The rest of the code in this article, explains how to retrieve Departments data from the database table using a REST API.
REST API Project - IDepartmentRepository Interface
These 2 methods perform database operations and we want them to be executed asynchronously so they return a task.
REST API Project - DepartmentRepository Class
REST API Project - Departments REST API Controller
Blazor Web Project - IDepartmentService Interface
Blazor Web Project - DepartmentService Class
Blazor Web Project - Startup.cs
In ConfigureServices method of the Startup class register HttpClient Services using AddHttpClient method.