Question MudBlazor simple syntax various errors in Cascade Dropdownlists

Pablo

Well-known member
Joined
Aug 12, 2021
Messages
62
Programming Experience
10+
Hello all,
I'm trying to move to MudBlazor starting by the page with the Cascade Dropdownlists, which before worked perfectly, but I know I have to upgrade to MudBlazor.
Here's a screenshot with the errors:
The error message says: Argument 2: Cannot convert from method group to Microsoft.AspNetCore.Components.EventCallback., and it highlights the method "@CountryHasChanged" of the ValueChanged. I can't figure why it just doesn't work like @onchange="CountryHasChanged" from simple pure Blazor. It also mark as an error this:
<MudSelectItem Value="">Please select a country ...</MudSelectItem> ... it doesn't like the empty string as a Value.
And here's the code:
C#:
@page "/"
@using MudBlazorClients.Models
@using MudBlazorClients.Services

<PageTitle>Index</PageTitle>

<h1>PLEASE SELECT A CLIENT</h1>

<div style="background-color:green">
    <label>COUNTRY:</label>
    <br />
    <MudSelect T="string" ValueChanged="@CountryHasChanged" Variant="Variant.Outlined">
        <MudSelectItem Value="">Please select a country ...</MudSelectItem>
        @foreach (var country in Countries)
        {
            <MudSelectItem Value="@country.CountryCode">@country.Name</MudSelectItem>
        }
    </MudSelect>
</div>
<div style="background-color:orange">
    <label>CITY:</label>
    <br />
    <MudSelect T="int" ValueChanged="@CityHasChanged">
        <MudSelectItem Value="0">Please select a city ...</MudSelectItem>
        @foreach (var city in Cities)
        {
            <MudSelectItem Value="@city.Id">@city.Name</MudSelectItem>           
        }
    </MudSelect>
</div>
<div style="background-color:brown">
    <label>CLIENT:</label>
    <br />
    <MudSelect T="int" ValueChanged="@ClientHasChanged">
        <MudSelectItem Value="0">Please select a client ...</MudSelectItem>
        @foreach (var client in Clients)
        {
            <MudSelectItem Value="@client.Id">@client.Name</MudSelectItem>
        }
    </MudSelect>
</div>
<div style="background-color:lightblue">
    <label>SELECTED CLIENT:</label>
    <br />
    <MudTextField Value="@client?.Name" ></MudTextField>
</div>

@code {
    private Client? client;

    private List<Country>? Countries;
    private List<City>? Cities;
    private List<Client>? Clients;

    CountryService countryService = new CountryService();
    CityService cityService = new CityService();
    ClientService clientService = new ClientService();

    protected override void OnInitialized()
    {
        Countries = countryService.GetAllCountries();
        Cities = new List<City>();
        Clients = new List<Client>();
    }

    private void CountryHasChanged(ChangeEventArgs e)
    {
        if (string.IsNullOrEmpty(e.Value.ToString()))
        {
            Cities.Clear();
        }
        else
        {
            LoadCities(e.Value.ToString());
        }
        Clients.Clear();
    }

    private void LoadCities(string countryCode)
    {
        Cities = cityService.GetCitiesByCountry(countryCode);
    }

    private void CityHasChanged(ChangeEventArgs e)
    {
        if (Convert.ToInt32(e.Value.ToString()) == 0)
        {
            Clients.Clear();
        }
        else
        {
            LoadClients(Convert.ToInt32(e.Value.ToString()));
        }
    }

    private void LoadClients(int cityId)
    {
        Clients = clientService.GetClientsByCity(cityId);
    }

    private void ClientHasChanged(ChangeEventArgs e)
    {
        if (Convert.ToInt32(e.Value.ToString()) > 0)
        {
            client = clientService.GetClientById(Convert.ToInt32(e.Value.ToString()));
        }
    }
}

I will highly appreciate your help to continue my web dev learning. Thank you.
Sorry for so silly questions, web dev is a new world for me.
Pablo
 

Attachments

  • index.png
    index.png
    110.2 KB · Views: 50
Hello, cjard
Sorry, I got the wrong version of the project. And I forgot you don't have the DB. It is that, as I don't see it, I forget it is there.
Now I make a minimal code that reproduces the runtime error. What I could not reproduce is the Service methods returning async Task. But the example works fine and crashes when selecting a country.
Thank you very much.
Pablo
 
Diff the attached against your files. If you don't have a diff tool, I recommend Beyond Compare. Install it, right click the first file, choose "select left file for compare", right click the second file and choose "compare to xxx"

Most significant change is that because youre using nullable ints in your MudSelect T=int? the thing you put in MudSelectItem Value=... also has to be nullable int. MudSelect cannot convert an int CityId, for example, to an int? CityId. In summary, change CityId and ClientId in City.cs and Client.cs so they are nullable too. Other edits tidy up your null handling
 

Attachments

  • Changes.zip
    1.9 KB · Views: 12
Helo, @cjard
I was able to run my app fine with your corrections to my code. Now I'll have to go back to my methods that load data from the DB.
I have to thank you very much for your time and being there giving me a great help in my learning.
Regards
Pablo
 

Latest posts

Back
Top Bottom