试图在控制器中过滤不起作用

时间:2018-12-14 05:44:34

标签: asp.net-core filter entity-framework-core

我已经通过搭建数据库来创建模型,然后通过实体框架核心创建了控制器

然后,我试图像这样在控制器的FullName方法中的Index字段上进行过滤:

public async Task<IActionResult> Index(string searchString)
{
    var nozContext = _context.NzAll.Include(n => n.CodeKNavigation).Include(n => n.CodeKhNavigation).Include(n => n.CodeMrNavigation).Include(n => n.CodeSexNavigation).Include(n => n.HalaNavigation);
    ViewData["CurrentFilter"] = searchString;
    var nzAllIQ = from s in _context.NzAll
                         select s;
    if (!String.IsNullOrEmpty(searchString))
    {
       nzAllIQ = nzAllIQ.Where(s => s.FullName.Contains(searchString));
    }
    return View(await nozContext.ToListAsync());
}

然后我将此表单放入index.cshtml:

<form asp-action="index" method="get">
   <div class="form-actions no-color">
      <p>
        Search by Name
        <input type="text" name="searchString" value= "@ViewData["CurrentFilter"]" />
        <input type="submit" value="Search" class="btn btn-default" />
       </p>
    </div>
</form>

但是当我按下“提交”按钮时,没有进行过滤!

为什么?以及如何解决?

3 个答案:

答案 0 :(得分:1)

问题在于您的返回值不是过滤后的数据 nzAllIQ

public async Task<IActionResult> Test(string searchString)
    {
        var nozContext = _context.NzAll.Include(n => n.CodeKNavigation).Include(n => n.CodeKhNavigation).Include(n => n.CodeMrNavigation).Include(n => n.CodeSexNavigation).Include(n => n.HalaNavigation);           
        ViewData["CurrentFilter"] = searchString;

        var nzAllIQ = from s in nozContext
                      select s;
        if (!String.IsNullOrEmpty(searchString))
        {
            nzAllIQ = nzAllIQ.Where(s => s.FullName.Contains(searchString));
        }
        return View(await nzAllIQ.ToListAsync());
    }

答案 1 :(得分:0)

print (df.filter(like='team').eq('kk'))
   team1  team2
0   True  False
1  False  False
2  False  False
3  False  False
4  False  False
5  False  False
6  False   True

print (df.filter(like='team').eq('kk').any(axis=1))
0     True
1    False
2    False
3    False
4    False
5    False
6     True
dtype: bool

您可以尝试这个还是

[HttpPost]
        public ActionResult TextSearch(string searchName)
        {
            var result = db.Jobs.Where(a => a.JobTitle.Contains(searchName)
            || a.JobContent.Contains(searchName)
            || a.Category.CategoryName.Contains(searchName)
            || a.Category.CategoryDescription.Contains(searchName)).ToList();

            return View(result);
        }

答案 2 :(得分:0)

您正在将过滤器应用于其他IQueriable变量。您执行nzAllIQ.Where...,但返回nozContext.ToListAsync()nozContext无法得知您的过滤意图。这可能会解决它,而无需过多更改代码。

public async Task<IActionResult> Index(string searchString)
{
    var nozContext = _context.NzAll
    .Include(n => n.CodeKNavigation)
    .Include(n => n.CodeKhNavigation)
    .Include(n => n.CodeMrNavigation)
    .Include(n => n.CodeSexNavigation)
    .Include(n => n.HalaNavigation);    

    ViewData["CurrentFilter"] = searchString;

    if (!String.IsNullOrEmpty(searchString))
    {
        nozContext = nozContext.Where(s => s.FullName.Contains(searchString));
    }
    return View(await nozContext.ToListAsync());
}

在不拖延自己的前提下,我为您提供了一些提示。尽量不要从控制器返回您的EF实体。通常,使用DTO或ViewModels(仅是普通类),而仅在其中投影视图所需的字段。这样,您可以节省数据库调用,而不必返回很多您不会使用的字段。