MudBlazor 自动完成搜索不区分大小写。 .NET 6 Blazor 服务器应用程序
您好,我在使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
按如下方式重构代码并检查是否返回正确的列表:
Refactor you code as follows and check that you are returning the correct list: