Home > Back-end >  Search function does not work in Asp.net Core
Search function does not work in Asp.net Core

Time:06-04

This is Most important thing to change without this search function does not work

Without this they does not find the string that you will search

And show Like this in your url localhost:7276/HomePage/Index?

First i use this

<input  type="text" placeholder="Search for..." aria-label="Search" aria-describedby="btnNavbarSearch" />

Second i use this

<input  type="text" placeholder="Search for..." name="SearchString" value="@ViewData["CurrentFilter"]" aria-label="Search" aria-describedby="btnNavbarSearch" />

<form method="get" action="/HomePage/Index" >
  <div >
    <input  type="text" placeholder="Search for..." name="SearchString" value="@ViewData["CurrentFilter"]" aria-label="Search" aria-describedby="btnNavbarSearch" />
    <input type="submit" value="Search"  />
  </div>
</form>

CodePudding user response:

Based on your previous question, it seems that you are trying to implement a search option at your nav bar. So you were missing this two properties in your view. name="SearchString" value="@ViewData["CurrentFilter"]".

How It works

When you submit the below form it will submit your user search input which has been set to SearchString as name property. So at your controller you will receive the value for string searchString which will filter your search result and return the new view

<form method="get" action="/Home/Index">
                
            <td style="padding-right:760px">

                </td>
            <td>
                <input  type="text" placeholder="Search for..." name="SearchString" value="@ViewData["CurrentFilter"]" aria-label="Search" aria-describedby="btnNavbarSearch" />
            </td>
            <td>
                <input type="submit" value="Search"  />
            </td>
</form>

Controller

      if (!String.IsNullOrEmpty(searchString))
            {
                members = members.Where(m => m.Name.Contains(searchString)
                                       || m.Gender.Contains(searchString));
                return View(members);
            }

Note: Above submitted keys name="SearchString" will be passed there and return the new view with the matched search key value result.

Output

enter image description here

  • Related