4

Here's the situation: i have a SearchPage where an user can make a complex search. Nothing really unusual. After the results are displayed, the user can select one of them and move to another Page (Like a Master/Detail).

I have a breacrumb which holds the places where the user has been and it can have more than 4 levels (Like Main -> 2Page -> 3Page -> 4Page -> NPage). What i want is to maintain the state of each control on my complex search page, if the user uses the breacrumb to navigate backwards, since i don't want them to manually set all those search filters again.

So far, i've been using javascript:history.back(), but since i can have multiple levels on my breadcrumb, this hasn't been very useful. I was thinking about using OutputCache to do it, but i don't know how i would proceed.

UPDATE

I've just talked to a co-worker and he told me that some of our combobox (dropdownlist) are dynamically generated. So if the user select one item on the first combobox, the second will be filled with data related to the first selection.

AdrianoRR
  • 1,131
  • 1
  • 17
  • 36

1 Answers1

4

OutputCache would cache the results for every user. Why don't you try to store the information in a cookie with page url and filter information. Each time an action is executed, read the cookie and populate the model (custom model for search) with those values found (if they match the page url, action in this situation). Pass the model to the view and let it repopulate the search criteria text boxes and check boxes.

UPDATE: When a user fills in the search filter text boxes, you are passing that information back to a controller somehow. Probably as some kind of a strongly typed object.

Let's say your users get to enter the following information: - Criteria - StartDate - EndDate

There is a model called SearchCriteria defined as:

public class SearchCriteria
{
    public string Criteria { get; set; }
    public DateTime? StartDate { get; set; }
    public DateTime? EndDate { get; set; }
}

Your action could look something like this:

[HttpGet]    
public ViewResult Search()
{
    SearchCriteria criteria = new SearchCriteria();

    if (Request.Cookies["SearchCriteria"] != null)
    {
        HttpCookie cookie = Request.Cookies["SearchCriteria"];
        criteria.Criteria = cookie.Values["Criteria"];
        criteria.StartDate = cookie.Values["StartDate"] ?? null;
        criteria.EndDate = cookie.Values["EndDate"] ?? null;
    }

    return View(criteria);
}

[HttpPost]
public ActionResult Search(SearchCriteria criteria)
{
    // At this point save the data into cookie
    HttpCookie cookie;

    if (Request.Cookies["SearchCriteria"] != null)
    {
        cookie = Request.Cookies["SearchCriteria"];
        cookie.Values.Clear();
    }
    else
    {
        cookie = new HttpCookie("SearchCriteria");
    }

    cookie.Values.Add("Criteria", criteria.Criteria);

    if (criteria.StartDate.HasValue)
    {
        cookie.Values.Add("StartDate", criteria.StartDate.Value.ToString("yyyy-mm-dd"));
    }

    if (criteria.EndDate.HasValue)
    {
        cookie.Values.Add("EndDate", criteria.EndDate.Value.ToString("yyyy-mm-dd"));
    }

    // Do something with the criteria that user posted

    return View();
}

This is some kind of a solution. Please understand that I did not test this and I wrote it from top of my head. It is meant to give you an idea just how you might solve this problem. You should probably also add Action to SearchCriteria so that you can check whether this is an appropriate action where you would read the cookie. Also, reading and writing a cookie should be moved into a separate method so that you can read it from other actions.

Hope this helps,

Huske

Huske
  • 9,186
  • 2
  • 36
  • 53
  • didn't know outputcache would cache it for every user. I was just reading about cookies. Any hints or links on how to use them with MVC3? – AdrianoRR Nov 18 '11 at 14:30
  • Thanks for your sample. How would proceed with dynamic DDL (dropdownlists)? I'm thinking on saving the entire SelectList on the cookie? Does that make any sense? – AdrianoRR Nov 18 '11 at 15:06
  • In that case add a property to SearchCriteria that returns SelectList. If the data in your drop down list is too big to be loaded every time from the database, place just that collection in a cache. You will have to reload the data from cache into your SelectList. Also, store just the value in the cookie, not the entire thing. Cookie is limited to 1024 characters (or something like that). – Huske Nov 18 '11 at 15:08
  • but storing only the value of the selected item will make DDL to have only one option. Should i recreate the SelectList and set the previous selected item as Selected? – AdrianoRR Nov 18 '11 at 15:39
  • Yes, that is why I said that you should cache the values that go into drop down list so that you don't have to reload them again, and then populate SelectList again plus apply Selected to the value that was in your cookie. – Huske Nov 18 '11 at 15:46