'If/else is not working properly with using OnInitiliazedAsync in Blazor C#
<div class="form-inline justify-content-center mt-4">
@if (Products != null && Products.Any())
{
@foreach (var product in Products)
{
#MyCode
}
}
else
{
<h2>This Store has no Products :)</h2>
}
</div>
@code
{
[Parameter]
public int Id { get; set; }
public IEnumerable<ProductDTO> Products { get; set; }
protected override async Task OnInitializedAsync()
{
Products = await Store.GetStoreProducts(Id);
}
}
It first show the else part and then load the if part ( if products are available ). It should work like that if products are available then else part shouldn't be loaded.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
