2011-11-01 63 views
0

在ASP MVC3 RedirectToAction殺死Session中的數據......爲什麼?ASP.MVC RedirectToAction殺死數據Session

請考慮這段代碼。

[HttpGet] 
public ActionResult RequestTestExtract() 
{ 
    return View(new ExtractRequestViewModel()); 
} 



[HttpPost] 
public ActionResult RequestTestExtract(ExtractRequestViewModel viewModel) 
{ 
    var currentExtracts = (Session["Extracts"] as Dictionary<string, bool>) ?? new Dictionary<string, bool>(); 
    currentExtracts.Add(viewModel.fileName, false); 
    Session["Extracts"] = currentExtracts; 

    // typing 
    // ?Session["Extracts"] 
    // in immediate window before RedirectToAction shows a value 
      // typing it after does not 
    return RedirectToAction("RequestTestExtract"); 
} 

如果我返回一個View而不是重定向到動作,Session仍然有我存儲在那裏的數據。

RedirectToAction非常重要,所以我將用戶留在GET頁面而不是POST頁面上,以避免出現惱人的轉發對話。

編輯:

這是我的控制器[SessionState的(SessionStateBehavior.ReadOnly)的頂部,當我刪除它的行爲正確。

+0

你有沒有試過使用'TempData'? –

+0

如果我沒有記錯的話,使用它的好處是什麼 – Peter

+0

如果我沒有記錯,TemData仍然是基於會話的。 –

回答

0

這是在我的控制器[SessionState(SessionStateBehavior.ReadOnly)]的頂部,當我刪除它時表現正常。