I am injecting my service in view to populate dropdown in razor like this
@inject Web.Services.MyService Options
Now I am trying to populate my dropdown like this
<select asp-for="CityID" asp-items="await Options.GetCities();" class="form-control form-control-sm">
<option>--Select--</option>
</select>
Getcities method is below
public async Task<IEnumerable<SelectListItem>> GetCities()
{
var cities = await _cityrepository.ListAllAsync();
var items = new List<SelectListItem>
{
new SelectListItem() { Value = null, Text = "All", Selected = true }
};
foreach (SetupCities city in cities)
{
items.Add(new SelectListItem() { Value = city.CityId.ToString(), Text = city.Name});
}
return items;
}
I am getting following error
NullReferenceException: Object reference not set to an instance of an object.
EvolvedX.Web.Services.VMWHService.GetCities() in VMWHService.cs
+
var cities = await _cityrepository.ListAllAsync();
AspNetCore.Views_Warehouse_CreateWarehouse.ExecuteAsync() in CreateWarehouse.cshtml
+
<select asp-for="CityID" asp-items="await Options.GetCities();" class="form-control form-control-sm">
I can see as the first await hit getcities the function exit. what is the proper way to handle such scenario M function is exiting before completing the method when it found await that is causing my selectlistitem to be null. I want to know how i modify my code so that selectlistitem get filled null exception will be solved