首页 > 解决方案 > 如何按字符串搜索

问题描述

我的网站现在可以显示数据。我想添加一个可以使用主键搜索字符串的函数。如何为我的网站创建搜索选项?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());
}

标签: postgresqlasp.net-core-mvc

解决方案


您可以检查以下代码:

在 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方法中,可以添加断点检查参数值是否正确,并检查查询结果。

截图如下:

在此处输入图像描述

更多详细信息,您可以查看教程示例代码


推荐阅读