MVC 5 会话在转到另一个视图时未被阻止



控制器调用另一个视图进行渲染后会话变量丢失。

public ActionResult Index(Customer model, string cancel, string effective)
{
if(!string.IsNullOrEmpty(cancel))
  {
    //update database
    Session["variable2"] = new Info(){ Text = "Do not processed"};
    return View("Cancelation");  //error stated occurs when calling another view
  }
 if(!string.IsNullOrEmpty(effective)
  {
     //do data base update
    Session["variable1"] = new Info(){ Text = "Processed"};
    return View(model); //All good here
  }
{

有一个MVC控制器,当回发到它时,我设置了一个会话变量"变量1",然后我确实返回View(模型)。在这种情况下,一切都很好,我可以在任何地方访问新的会话变量 1。

但是当我再次回发到同一个控制器时,我检查单击的按钮,然后我设置了另一个会话变量"variable2",这次我确实返回View("Cancelation")

最后一个变量 2 丢失,并且不会显示在应用程序中任何位置HttpContext.Current.Session["variable2"]上。

有人可以帮助理解为什么吗?

我发现因为我使用 SQL Server 作为会话存储,所以我必须在对象上使用 Serialize() 属性,然后它就可以工作了。