在.NET MVC preserve数据数据、NET、MVC、preserve

2023-09-04 22:35:50 作者:热血少年.

我在执行与结果页支持分页功能的搜索模块。 通过的NerdDinner提供的示例将页面编号为索引操作的参数,和操作使用页面编号进行查询用户每次打了一个不同的页码。

I am implementing a search module with result page support paging. The example provided by NerdDinner passes pagenumber as a parameter for the Index action, and the action uses the pagenumber to perform a query each time the user hit a different page number.

我的问题是,我的搜索​​需要许多条件,如价格,材质,型号等,不仅仅是简单的页面编号。所以,我想preserve的标准后,用户第一次提交的,所以我只需要通过页面编号来回。

My problem is that my search take many more criteria such as price, material, model number etc. than just simple pagenumber. Therefore, I would like to preserve the criteria after users' first submission, so that I only have to pass the pagenumber back and forth.

使用的ViewData是不可能的,因为的ViewData得到清除,一旦它被发送到观

Using ViewData is not possible because ViewData get cleared once it is sent to the View.

有没有什么好办法preserve的标准数据,我想?

Is there any good way to preserve the criteria data as I wish?

推荐答案

您pretty的多少有两种方法可以做到这一点。

You pretty much have two ways to do this.

将要preserve在一个序列化的会话,缓存或数据库中的数据。把它在数据库将是最安全的选择,但它会降低你的表现。

Put the data that you want to preserve in a serializable session, cache or the database. Putting it in the database will be the safest choice but it will degrade your performance.

可以存储$ P $的隐藏的HTML标签pserved数据。只要信息不敏感此选项应该很好地工作。

You can store the preserved data in a hidden html tag. As long as the information is not sensitive this option should work well.

下面是一些支持code。您只能在同一个控制器中使用

here is some supporting code. You can only use this within the same controller

public class questionController : Controller
{
    public QuestionFormData qdata;

    protected override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        qdata = (SerializationUtil.Deserialize(Request.Form["qdata"])
            ?? TempData["qdata"]
            ?? new QuestionFormData()) as QuestionFormData;
        TryUpdateModel(qdata);
    }

    protected override void OnResultExecuted(ResultExecutedContext filterContext)
    {
        if (filterContext.Result is RedirectToRouteResult)
        {
            TempData["qdata"] = qdata;
        }
    }

这样的访问更新的信息

Access the updated information like this

    public ActionResult Index()
    {
         DateTime d = qdata.date;
    }

在aspx页面

<%= Html.Hidden("qdata", SerializationUtil.Serialize(Model)) %>