postgresql - 如何按字符串搜索
问题描述
我的网站现在可以显示数据。我想添加一个可以使用主键搜索字符串的函数。如何为我的网站创建搜索选项?sb可以帮我发一下教材吗?我已经读过了。但我仍然不知道。 https://docs.microsoft.com/en-us/aspnet/core/data/ef-mvc/sort-filter-page?view=aspnetcore-5.0。我也尝试过这种方式。但我不知道为什么我的“包含”不能使用。
public async Task<IActionResult> Index(string searchString)
{
var movies = from m in _context.Movie
select m;
if (!String.IsNullOrEmpty(searchString))
{
movies = movies.Where(s => s.Title.Contains(searchString));
}
return View(await movies.ToListAsync());
}
解决方案
您可以检查以下代码:
在 Index View Page 中:通过 Get 方法将表单提交给 Index Action,对于搜索文本框,命名为searchString
. 对于“搜索”按钮,请使用以下submit
类型:
<form asp-action="Index" method="get">
<div class="form-actions no-color">
<p>
Find by name: <input type="text" name="searchString" value="@ViewData["CurrentFilter"]" />
<input type="submit" value="Search" class="btn btn-default" /> |
<a asp-action="Index">Back to Full List</a>
</p>
</div>
</form>
然后,在Index action方法中,可以添加断点检查参数值是否正确,并检查查询结果。
截图如下:
推荐阅读
- c++ - 使用 glm::perspective 时,如何将鼠标实际像素的位置映射到标准化的位置?
- reactjs - javascript数组如何用新行显示结果
- python - 将 selenium 和 Beautiful soup 中的多个字符串转换为 CSV 文件
- multithreading - 将带有引用的迭代器移动到线程中
- python - 如何使用 python 使用勇敢的浏览器启动 selenium web 驱动程序
- flutter - 你如何在颤振飞镖中渲染脚本标签
- python - 单行上的 matplotlib 动画组
- php - 无法运行 INSERT 查询,因为 db 已锁定 - SQLite
- python - 如何在熊猫日期时间数据框中索引打开和关闭?
- python - 从修饰函数中获取函数参数和变量名称和值