gourav kumar
gourav kumar

Reputation: 155

Search function does not work in Asp.net Core

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 class="form-control" type="text" placeholder="Search for..." aria-label="Search" aria-describedby="btnNavbarSearch" />

Second i use this

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

<form method="get" action="/HomePage/Index" class="d-none d-md-inline-block form-inline ms-auto me-0 me-md-3 my-2 my-md-0">
  <div class="input-group sty">
    <input class="form-control" type="text" placeholder="Search for..." name="SearchString" value="@ViewData["CurrentFilter"]" aria-label="Search" aria-describedby="btnNavbarSearch" />
    <input type="submit" value="Search" class="btn btn-primary" />
  </div>
</form>

Upvotes: 2

Views: 690

Answers (1)

Md Farid Uddin Kiron
Md Farid Uddin Kiron

Reputation: 22447

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 class="form-control" type="text" placeholder="Search for..." name="SearchString" value="@ViewData["CurrentFilter"]" aria-label="Search" aria-describedby="btnNavbarSearch" />
  </td>
  <td>
    <input type="submit" value="Search" class="btn btn-primary" />
  </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

Upvotes: 2

Related Questions