如何按字符串搜索

How to search by string

我的网站现在可以显示数据了。 我想添加一个可以使用主键搜索字符串的功能。 如何为我的网站创建搜索选项? 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页面:通过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方法中,可以加断点检查参数值是否正确,检查查询结果。

截图如下:

更多详细信息,您可以查看the tutorial and the sample code