ASP.NET MVC: Keeping last page state

最后都变了- 提交于 2019-12-19 09:09:10

问题


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:


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



来源:https://stackoverflow.com/questions/8183790/asp-net-mvc-keeping-last-page-state

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!