我知道这个主题已经在很多帖子中得到了处理,但我无法解决这个问题。
在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");
}
}
答案 0 :(得分:16)
存储向导:
Session["object"] = wiz;
获取向导:
WizardProductVM wiz = (WizardProductVM)Session["object"];
答案 1 :(得分:2)
如果您只在下一个操作中需要它并且您打算再次存储它,则可以使用TempData。 TempData与Session基本相同,只是它在下次访问时被“删除”,因此需要再次按照你的指示进行存储。
http://msdn.microsoft.com/en-us/library/dd394711(v=vs.100).aspx
如果可能的话,最好确定一种方法来使用已发布的参数传递必要的数据而不是依赖于会话(tempdata或其他)