locked
Select/Dropdownlist in Blazor RRS feed

  • Question

  • User2041008840 posted

    How Can I bind the dropdownlist in Blazor Server, It showing Error Object Refrence not set to an instant object.
    Please help Me 

    <select class="form-control selectpicker" @bind="Employee.CountryID">
                     <option>--select--</option>
                     @foreach(var item in Countries)
                         {
                             <option>@item.Name</option>
                         }
                 </select>
    Country[] Countries;
       public Employee Employee { get; set; }
       Employee[] employees;
       //Department[] Departments;
     
       protected override async Task OnInitializedAsync()
       {
           Employee = new Employee();
           await load();
       }
     
       protected async Task load()
       {
           Countries = await CountriesService.GetCountryAsync();
           employees = await EmployeesService.GetEmployeesAsync();
       }
    Tuesday, March 17, 2020 6:10 PM

All replies

  • User415553908 posted

    assuming your Country class has some sort of Id, you just need to supply it as value to respective select options:

    <select class="form-control selectpicker" @bind="Employee.CountryID">
    <option>--select--</option>
    @foreach (var item in Countries)
    {
    <option value="@item.Id">@item.Name</option> // Blazor needs to know what values it binds to!
    }
    </select>
    Friday, March 20, 2020 5:31 PM