trying to filter in controller does not work

asp.net-core entity-framework-core filter

Question

I've created a model by scaffolding my database and then created the controller by entity framework core

Then I am trying to make a filter on FullName field in Index method in the controller like this:

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());
}

Then I put this form in 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>

But when I press submit button no filtering is done!

Why? and how to solve please?

1
0
12/14/2018 9:00:20 AM

Popular Answer

The problem is that your return value is not fiiltered data 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
12/14/2018 9:37:44 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow