0
votes

In ASP MVC3 RedirectToAction kills data in Session...WHY?

Consider this code.

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

If I return a View instead of redirect to action the Session still has the data I stored there.

The RedirectToAction is important so I leave the user on a GET page rather than a POST page to avoid that annoying repost dialogue that comes up.

EDIT:

This was at the top of my controller [SessionState(SessionStateBehavior.ReadOnly)] when I delete this it behaves properly.

1
have you tried to use TempData? - Daniel A. White
What is the advantage of using that over Session - Peter
If I remember correctly TemData is still session based. - Code Jammr

1 Answers

0
votes

This was at the top of my controller [SessionState(SessionStateBehavior.ReadOnly)] when I delete this it behaves properly.