ASP.NET MVC:保持最后一页的状态 [英] ASP.NET MVC: Keeping last page state

查看:109
本文介绍了ASP.NET MVC:保持最后一页的状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的情况:我有一个SearchPage凡用户能进行复杂的搜索。真的没什么不寻常的。结果显示后,用户可以选择其中之一,并移动到另一个页面(就像一个主/详细信息)。

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).

我有它保存在用户已经去过的地方一个breacrumb,它可以有超过4级(像主 - >第2页 - >第3页 - > 4页 - > NPAGE)。我要的是保持我的复杂的搜索页面上每个控件的状态,如果用户使用的breacrumb向后导航,因为我不想让他们手动重新设置所有的搜索过滤器。

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.

到目前为止,我一直在使用的javascript:history.back(),但因为我可以有我的痕迹多个级别,这一直都不是很有益的。我想使用的OutputCache 来做到这一点,但我不知道我怎么会继续。

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.

更新

我刚刚跟一个同事,他告诉我,我们的一些组合框(DropDownList的)的动态生成。因此,如果用户在第一组合框选择一个项目时,第二将充满相关的第一选择数据

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.

推荐答案

的OutputCache 将缓存为每一位用户的结果。你为什么不尝试存储在页面URL中的Cookie中的信息并过滤信息。每次执行一个动作时,读取cookie和填充发现这些值模型(搜索自定义模型)(如果他们在这种情况下匹配的页面URL,动作)。通过该模型视图,让它重新填充搜索标准文本框和复选框。

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

有一个名为SearchCriteria模型定义如下:

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();
}

这是某种解决方案。请理解,我没有测试这一点,我写它从我的头顶。它的目的是给你你可能只是怎么解决这个问题的想法。你应该还添加操作SearchCriteria,让您可以检查这是否是一个适当的行动,你会读取cookie。另外,读,写一个cookie应该被移动到一个单独的方法,这样就可以从其他动作读取。

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.

希望这有助于

Huske

这篇关于ASP.NET MVC:保持最后一页的状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆