Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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>

>Solution :

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

Output

enter image description here

Hope now it is completely clear to you. and It will resolve your issue.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading