MudBlazor 自动完成搜索不区分大小写。 .NET 6 Blazor 服务器应用程序

发布于 2025-01-14 17:02:33 字数 1677 浏览 4 评论 0原文

您好,我在使用 MudBLazor 自动完成功能搜索给定列表时遇到问题。 此国家/地区列表是从列表服务中获取的。 我尝试将 StringComparison.InvariantCultureIgnoreCase 更改为其他文化,但没有任何运气!

这是代码: 我真的不明白为什么自动完成功能不返回与搜索字符串(值)匹配的较小列表。它仅与区分大小写的字母匹配,例如 Albania,而不是 albania。

  <MudCard Style="margin-bottom: 20px; width: 600px;">
                            <MudCardContent>

                                <label>Sending from:</label>
                                <MudAutocomplete T="string" Label="Country" @bind-Value="@userCountry" SearchFunc="@SearchFromCountries" />
                            </MudCardContent>
                        </MudCard>
@code {

    private string userCountry, userToCountry;
    public IEnumerable<CountriesCurrencies> countries;
  
    private List<string> countryList;

    protected override async Task OnInitializedAsync()
    {
        userCountry = await countryService.GetUserCountry(userId);
        countries = await countryService.GetCountriesList();
        countryList = GetCountriesList();
    }

    private List<string> GetCountriesList()
    {
        countryList = new List<string>();
        foreach (var country in countries)
        {
            countryList.Add(country.Country);
        }
        return countryList;
    }

    private async Task<IEnumerable<string>> SearchFromCountries(string value)
    {
        await Task.Delay(5);

        if (string.IsNullOrEmpty(value))
            return countryList;

        return await Task.FromResult(countryList.Where(x => x.Contains(value, StringComparison.InvariantCultureIgnoreCase)));

    }

Hi I have problems making MudBLazor autocomplete to search a given list.
This list of countries is fetched from a service as List.
I have tried changing the StringComparison.InvariantCultureIgnoreCase to other culture without any luck!

Here is the code:
I really dont understand why autocomplete is not returning a smaller list matching the search string (value). It only matches with case-sensitive letters like Albania not albania.

  <MudCard Style="margin-bottom: 20px; width: 600px;">
                            <MudCardContent>

                                <label>Sending from:</label>
                                <MudAutocomplete T="string" Label="Country" @bind-Value="@userCountry" SearchFunc="@SearchFromCountries" />
                            </MudCardContent>
                        </MudCard>
@code {

    private string userCountry, userToCountry;
    public IEnumerable<CountriesCurrencies> countries;
  
    private List<string> countryList;

    protected override async Task OnInitializedAsync()
    {
        userCountry = await countryService.GetUserCountry(userId);
        countries = await countryService.GetCountriesList();
        countryList = GetCountriesList();
    }

    private List<string> GetCountriesList()
    {
        countryList = new List<string>();
        foreach (var country in countries)
        {
            countryList.Add(country.Country);
        }
        return countryList;
    }

    private async Task<IEnumerable<string>> SearchFromCountries(string value)
    {
        await Task.Delay(5);

        if (string.IsNullOrEmpty(value))
            return countryList;

        return await Task.FromResult(countryList.Where(x => x.Contains(value, StringComparison.InvariantCultureIgnoreCase)));

    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

看透却不说透 2025-01-21 17:02:33

按如下方式重构代码并检查是否返回正确的列表:

    private Task<IEnumerable<string>> SearchFromCountries(string value)
    {
        if (string.IsNullOrEmpty(value))
            return countryList;

        var list = countryList.Where(x => x.Contains(value, StringComparison.InvariantCultureIgnoreCase));
       // break point here to check what you are returning
        return Task.FromResult(list);

    }

Refactor you code as follows and check that you are returning the correct list:

    private Task<IEnumerable<string>> SearchFromCountries(string value)
    {
        if (string.IsNullOrEmpty(value))
            return countryList;

        var list = countryList.Where(x => x.Contains(value, StringComparison.InvariantCultureIgnoreCase));
       // break point here to check what you are returning
        return Task.FromResult(list);

    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文