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

查看:11
本文介绍了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 个级别(如 Main -> 2Page -> 3Page -> 4Page -> 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.

更新

我刚刚与一位同事交谈,他告诉我我们的一些组合框(下拉列表)是动态生成的.因此,如果用户在第一个组合框中选择一个项目,第二个将填充与第一个选择相关的数据.

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

这是某种解决方案.请理解,我没有对此进行测试,而是从头顶上写下来的.它旨在让您了解如何解决此问题.您可能还应该将 Action 添加到 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.

希望这会有所帮助,

哈士奇

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

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