8

このテーマは数多くの投稿で扱われていることは知っていますが、うまくいきません。

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");
        }
    }
4

2 に答える 2

17

ウィザードの保管:

Session["object"] = wiz;

ウィザードの取得:

WizardProductVM wiz = (WizardProductVM)Session["object"];
于 2012-08-09T14:20:18.757 に答える
2

次のアクションでのみ必要で、再度保存する予定がある場合は、TempData を使用できます。TempData は、次のアクセス時に「削除」されることを除いて、基本的に Session と同じです。したがって、行っていることを示したように再度保存する必要があります。

http://msdn.microsoft.com/en-us/library/dd394711(v=vs.100).aspx

ただし、可能であれば、セッション (tempdata など) に依存するのではなく、投稿されたパラメーターを使用して必要なデータを渡す方法を決定することをお勧めします。

于 2012-08-09T14:19:48.957 に答える