I am building a Blazor client side application. I am using MatBlazor components.
I have two MatSelectString controls on a page. The first one is used to select a category, and the second one to select products from the category. So I have it setu up like so:
<MatSelect Outlined="true" Label="Category" @bind-Value="@Category">
<MatOptionString></MatOptionString>
@foreach (var cat in GetCategories())
{
<MatOptionString Value="@cat">@cat</MatOptionString>
}
</MatSelect>
<MatSelect Outlined="true" Disabled="@(!string.IsNullOrWhiteSpace(Category))" Label="Product" @bind-Value="@Product" >
<MatOptionString></MatOptionString>
@foreach (var prod in GetProducts(Category))
{
<MatOptionString Value="@prod">@prod</MatOptionString>
}
</MatSelect>
Within the GetProducts(Category)
code, I want to call the backend. The issue is that there is only a HttpClient.GetJsonAsync<>()
method, that cannot be called from within a non-async method. But GetProduct() cannot be made async.
Things I have tried:
Any ideas?
You can use ValueChanged
EventCallback:
<MatSelect
Outlined="true"
Label="Category"
ValueChanged="(string i)=>OnChangeCategory(i)"> @* <-- here *@
<MatOptionString></MatOptionString>
@foreach (var cat in GetCategories())
{
<MatOptionString Value="@cat">@cat</MatOptionString>
}
</MatSelect>
@code
{
protected async Task OnChangeCategory(string newValue)
{
this.Category = newValue;
// call backend async tasks
// ...
}
Check it out at blazorfiddle.com