在 ASP.NET Core Web 应用程序中安装 Blazored Typeahead。
在 Razor 页面中导入 Blazored Typeahead 和 System.Net.Http。
@using Blazored.Typeahead
@using System.Net.Http
创建一个新的组件,用于返回与已键入字符串匹配的值。
public class Typeahead
{
private HttpClient _http;
public Typeahead(HttpClient http)
{
_http = http;
}
public async Task> Search(string searchText)
{
return await _http.GetFromJsonAsync>($"/api/search/{searchText}");
}
}
上述组件将 searchString 作为参数使用,然后通过使用 HttpClient 发送 HTTP GET 请求来向搜索 API 发送搜索信息。返回的 IEnumerable 包含所有与输入的搜索字符串匹配的值。
在 Razor 页面中使用 Blazored Typeahead,指定搜索 API 和选定的值。
在上面的代码中,SearchMethod 属性指定了我们在第三步中创建的组件方法。ValueProperty 和 TextProperty 属性表示在选定项中使用的属性。@bind-Value 绑定选定项的属性值,而 Placeholder 属性显示在文本框中的占位符文本。
在完成这些步骤后,您现在应该能够使用 Blazored Typeahead 在 ASP.NET Core Web 应用程序中导入数据库中选中的值。