在Session中存储对象

Arn*_*501 6 asp.net-mvc-3

我知道这个主题已经在很多帖子中得到了处理,但我无法解决这个问题.

在一个Controller里面一个ActionResult我想在Session中存储一个对象并在另一个ActionResult中检索它.像那样 :

    public ActionResult Step1()
    {
        return View();
    }

    [HttpPost]
    public ActionResult Step1(Step1VM step1)
    {
        if (ModelState.IsValid)
        {
            WizardProductVM wiz = new WizardProductVM();
            wiz.Step1 = step1;
            //Store the wizard in session
            // .....
            return View("Step2");
        }
        return View(step1);
    }

    [HttpPost]
    public ActionResult Step2(Step2VM step2)
    {
        if (ModelState.IsValid)
        {
            //Pull the wizard from the session
            // .....
            wiz.Step2 = step2;
            //Store the wizard in session again
            // .....
            return View("Step3");
        }
    }
Run Code Online (Sandbox Code Playgroud)

Cal*_*Yau 16

存储向导:

Session["object"] = wiz;
Run Code Online (Sandbox Code Playgroud)

获取向导:

WizardProductVM wiz = (WizardProductVM)Session["object"];
Run Code Online (Sandbox Code Playgroud)