1

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:

  • Putting the call to my function inside an async lambda (not allowed in foreach or in other code blocks)
  • Using Task.Result (it hangs)
  • Putting the back-end call in other component level async events (gets called multiple times)

Any ideas?

1 Answer 1

4

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

5
  • Extra points for the elegant casting of the integer to a string inline using: "(string i)=>" 👍🏾 Commented Dec 31, 2019 at 18:57
  • Thanks @MichaelWashington appreciate 🙌 Most Matblazor controls are generic, without explicit type on anonymous function it is unable to inferring type. Commented Dec 31, 2019 at 19:16
  • @daniherrera It would be great if you submitted a PR on MatBlazor for an example of this.
    – Enkode
    Commented Jan 2, 2020 at 17:23
  • @daniherrera I am trying your code in a WebAssembly project and I am receiving this error "Unhandled exception rendering component: MatBlazor.MatSelect1[System.Nullable1[System.Int64]] requires a value for the 'ValueExpression' parameter. Normally this is provided automatically when using 'bind-Value'.". Any idea why it requires also a ValueExpression parameter ?
    – fededim
    Commented Sep 7, 2020 at 9:05
  • Here it is my code <MatSelect Label="@strings.Authority" ValueChanged="(Int64? nv)=>OnChangeAuthority(nv)" > @if (Authorities != null) @foreach (var au in Authorities) { <MatOption TValue="Int64?" Value="@au.Id">@au.Name</MatOption> } </MatSelect>
    – fededim
    Commented Sep 7, 2020 at 9:06

Not the answer you're looking for? Browse other questions tagged or ask your own question.