Creating this as I looked at MVC4 Session not persisted between requests and various others but none of them helped to solve my issue.
I have the following two action methods on my MVC controller:
public ActionResult Capture(string testValue)
{
Session["Test"] = testValue;
ViewBag.Test = Session["Test"]; // This works and correctly sets the value
return View();
}
[HttpPost]
public ActionResult Capture(SomeViewModel viewModel)
{
ViewBag.Test = Session["Test"]; // At this point the value in the session is inexplicably null, and thus so is the item in the ViewBag
return View(viewModel);
}
When I try and use ViewBag.Test
in my view
- On the initial Get, it works
- On the Post, it is null.
The same behaviour if I set a breakpoint in the code, the session object is set and has the right value in the initial "Get" but is null in the Post after.
What is happening to my session, why is the value not being persisted between requests?
I have established that the session is being created on every request but cannot see why.