首页 > 解决方案 > Blazored Typeahead ConvertMethod 不起作用

问题描述

我想使用 Blazored.Typeahead 组件,但由于 ConvertMethod 它返回此错误:

严重性代码 描述 项目文件行抑制状态错误 CS0411 方法 'TypeInference.CreateBlazoredTypeahead_0<TItem, TValue> 的类型参数(RenderTreeBuilder, int, int, Func<string, Task<IEnumerable>>, int, Func<TItem, TValue>, int, TValue, int, EventCallback, int, Expression<Func>, int, RenderFragment, int, RenderFragment)' 不能从用法中推断出来。尝试明确指定类型参数。

<BlazoredTypeahead SearchMethod="GetRegionByTown" 
                   ConvertMethod="ConvertTown"
                   @bind-Value="RegionReport.RegionName">
    <SelectedTemplate Context="region">@region.RegionName</SelectedTemplate>
    <ResultTemplate Context="town">@town.Name</ResultTemplate>
</BlazoredTypeahead>
<ValidationMessage For="@(() => RegionReport.RegionName)" />

public class Town
{
    [Key]
    public string Code { get; set; }
    public string Name { get; set; }
}

public class RegionReport
{
    [Required]      
    public string RegionName { get; set; }
}

protected async Task<string> ConvertTown(Town town)
{
    RegionReport.RegionName = town?.Name;
    return await Task.FromResult(town?.Name);
}

protected async Task<IEnumerable<Town>> GetRegionByTown(string townName)
{
   return await RegionService.GetRegionByTown(townName);
}

请问这段代码有什么问题?

解决方案

<BlazoredTypeahead SearchMethod="GetRegionByTown" 
                   ConvertMethod="ConvertTown"
                   @bind-Value="RegionReport.RegionName">
    <SelectedTemplate Context="region">@region</SelectedTemplate>
    <ResultTemplate Context="town">@town.Name</ResultTemplate>
</BlazoredTypeahead>
<ValidationMessage For="@(() => RegionReport.RegionName)" />

public class Town
{
    [Key]
    public string Code { get; set; }
    public string Name { get; set; }
}

public class RegionReport
{
    [Required]      
    public string RegionName { get; set; }
}

protected string ConvertTown(Town town)
{
    return town?.Name;
}

protected async Task<IEnumerable<Town>> GetRegionByTown(string townName)
{
   return await RegionService.GetRegionByTown(townName);
}

标签: typeaheadblazored

解决方案


推荐阅读