5

I have a search box that searches by using EMPLID. Search works but if I go to any other page after doing search (Ex: If I switch to details page) and then navigate back to the page I did search on, It displays all the records. How can I keep the search criteria so that when I navigate between pages it shows information of that EMPLID?

My controller:

public ActionResult Index(string SearchString)
{

    var emp = from e in db.EMPLOYMENTs
              select e;

    if (!String.IsNullOrEmpty(SearchString))
    {
        emp = emp.Where(s => s.EMPLID.Contains(SearchString));
    }

    return View(emp);
}

My Layout:

@using (Html.BeginForm())
{
    <form class="navbar-form navbar-left" role="search">
        <div class="form-group">
            <input class="form-control" placeholder="Search" type="text" name="SearchString">
        </div>
    </form>
}
jomsk1e
  • 3,585
  • 7
  • 34
  • 59
mevren
  • 85
  • 1
  • 7
  • You'll likely want to stuff that data into a session variable, and then utilize that on each page if present. – CollinD Sep 15 '15 at 21:51
  • i guess using cookies is best way to keep data while switching between pages – A.T. Sep 16 '15 at 04:59

2 Answers2

1

You can use TempData for preserving the search string.

Add your SearchString in temp data like this-

TempData["SearchString"] = SearchString;

...and get back the value when required-

string searchString = TempData["SearchString"] as string;

Please refer this msdn article for more information on passing Data in an ASP.NET MVC Application

Yogi
  • 9,174
  • 2
  • 46
  • 61
0

You have two options: to use TempData or to use Session State.

Follow the examples:

http://www.dotnetcurry.com/aspnet-mvc/1074/aspnet-mvc-pass-values-temp-data-session-request

http://www.codeproject.com/Articles/818493/MVC-Tempdata-Peek-and-Keep-confusion

TempData keep() vs peek()

How to keep search value after delete in MVC

Community
  • 1
  • 1
Renatto Machado
  • 1,534
  • 2
  • 16
  • 33