ASP.NET MVC: Maintaining the State of the Last Page

Here's the situation: I have SearchPage where the user can perform a complex search. Nothing unusual. After displaying the results, the user can select one of them and go to another page (for example, a master / part).

I have a slot that contains places where the user was, and can have more than 4 levels (for example, Main β†’ 2Page β†’ 3Page β†’ 4Page β†’ NPage). I want to keep the state of each control on my complex search page if the user uses breacrumb to move backward, since I do not want them to manually set all these search filters again.

So far I have used javascript:history.back() , but since I can have several levels on my breading, this is not very useful. I was thinking about using OutputCache to do this, but I don't know how I will continue.

UPDATE

I just talked with an employee and he told me that some of our combobox (dropdownlist) are dynamically generated. Therefore, if the user selects one item in the first combo box, the second will be filled with data associated with the first selection.

+4
source share
1 answer

OutputCache will cache the results for each user. Why not try to save the information in a cookie with information about the URL and filter. Each time the action is performed, read the cookie and fill out the model (user model for search) with the values ​​found (if they correspond to the URL of the page, the action is in this situation). Pass the model into the view and let it update the text fields of the search criteria and check boxes.

UPDATE: When the user fills in the text fields of the search filter, you somehow pass this information back to the controller. This is probably some kind of strongly typed object.

Say your users can enter the following information: - Criteria - Start date - 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 might 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 solution. Please understand that I have not tested this, and I wrote it on top. It is designed to give you an idea of ​​how you can solve this problem. You should probably also add Action to SearchCriteria so that you can check if this is the appropriate action when you read the cookie. In addition, reading and writing a cookie should be moved to a separate method so that you can read it from other actions.

Hope this helps,

Huske

+4
source

Source: https://habr.com/ru/post/1381913/


All Articles