.net 为什么MudSelectItem中的OnClick未被触发?

fdbelqdn  于 2023-05-19  发布在  .NET
关注(0)|答案(1)|浏览(159)

我需要选择我想要的制造商,然后在此之后,我需要检索的某些制造商所需的所有型号。

@if (manufacturersRetrieved)
{
    <MudTextField T="string" Label="Filter Manufacturer" Variant="Variant.Outlined" @bind-Value="manufacturerFilter" @oninput="ApplyManufacturerFilter"></MudTextField>
    <MudSelect T="ManufacturerDto" @bind-Value="selectedManufacturer" Label="Select desired Manufacturer" Search="true" Variant="Variant.Outlined">
        @foreach (var option in filteredManufacturers)
        {
            <MudSelectItem T="ManufacturerDto" OnClick="() => RetrieveModels(option.ManufacturerId)" Value="@option">@option.ManufacturerName</MudSelectItem>
        }
    </MudSelect>
    <MudButton Class="ma-1" Variant="Variant.Filled" Color="Color.Secondary" OnClick="GetManufacturers">Get All Manufacturers</MudButton>
    @if (modelsRetrieved)
    {
        @if (filteredModels == null)
        {
            <MudProgressCircular Color="Color.Default" Indeterminate="true"/>
        }
        else
        {
            <MudTextField T="string" Label="Filter Model" Variant="Variant.Outlined" @bind-Value="modelFilter" @oninput="ApplyModelFilter"></MudTextField>
            <MudSelect T="ModelDto" Label="Select desired Model" @bind-Value="selectedModel" Search="true" Variant="Variant.Outlined">
                <Content>
                    @foreach (var option in filteredModels)
                    {
                        <MudSelectItem T="ModelDto" Value="@option">@option.ModelName</MudSelectItem>
                    }
                </Content>
            </MudSelect>
        }
    }
}

@code {
    private IEnumerable<ManufacturerDto> manufacturers = new List<ManufacturerDto>();
    private IEnumerable<ManufacturerDto> filteredManufacturers = new List<ManufacturerDto>();
    private IEnumerable<ModelDto> models = new List<ModelDto>();
    private IEnumerable<ModelDto> filteredModels = new List<ModelDto>();
    private string manufacturerFilter = "";
    private string modelFilter = "";
    private bool manufacturersRetrieved = false;
    private bool modelsRetrieved = false;
    private ManufacturerDto selectedManufacturer = null;
    private ModelDto selectedModel = null;

    protected override void OnInitialized()
    {
        manufacturers = _iDataService.GetManufacturersFromLocalData().OrderBy(x => x.ManufacturerName);
        manufacturersRetrieved = true;

        ApplyManufacturerFilter(null);
    }

    private void ApplyManufacturerFilter(ChangeEventArgs e)
    {
        filteredManufacturers = manufacturers.Where(x => x.ManufacturerName.ToLower().Contains(manufacturerFilter.ToLower()));
    }

    private async Task GetManufacturers()
    {
        manufacturers = await _iCarService.GetManufacturersFromApi();
        filteredManufacturers = manufacturers.OrderBy(x => x.ManufacturerName).Where(x => x.ManufacturerName.ToLower().Contains(manufacturerFilter.ToLower()));
    }
    
    private async Task RetrieveModels(long manufacturerId)
    {
        models = (await _iCarService.GetModelsOnManufacturerId(manufacturerId)).OrderBy(x => x.ModelName).ToList();
        modelsRetrieved = true;
        ApplyModelFilter(null);
    }

    private void ApplyModelFilter(ChangeEventArgs e)
    {
        modelFilter = e.Value.ToString();
        filteredModels = models.Where(x => x.ModelName.ToLower().Contains(modelFilter.ToLower()));
    }
}

我不能解决这个问题。我需要这样做。
我试过很多方法,但这一种似乎是最有效的。
我试着在MudSelect中创建OnClose,但它不够好。我甚至试着把所有东西都列成按钮,但没有。

sirbozc5

sirbozc51#

试试这个

OnClick="@(() => RetrieveModels(option.ManufacturerId))"

相关问题