ASP.NET MVC: Keeping last page state

2019-02-18 10:27发布

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.

1条回答
【Aperson】
2楼-- · 2019-02-18 11:11

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

查看更多
登录 后发表回答