0

I'm trying to store an object temporarily so that it can then be used in the next action method, and I'm aiming to achieve this by using TempData or Session (Since TempData is really just Session under the hood).

I'm running into an issue though, in one part of my code I have an action method like this:

public ActionResult PlaceHolderActionName()
{
    //Do some processing here...

    TempData["TempModel"] = new TempModel 
    { 
       TempMessage = "Message here.", 
       TempStatus = true 
    };

    return RedirectToAction("ActionMethodName", "Home");
}

And then I'm trying to access the TempData in the action method this is redirecting to like this:

public ActionResult ActionMethodName()
{
    TempModel tempModel = new TempModel();
    if(TempData["TempModel"] != null)
    {
        tempModel = (TempModel)TempData["TempModel"];
    }

    return View(tempModel);
}

However, having stepped through the code, I can see that as soon as the RedirectToAction method is called, the Session and TempData variables are both cleared. This does not happen when the View method is used, only on RedirectToAction?

For some clarity, This is where I found this idea of using TempData on a RedirectToAction, but it appears the TempData solution discussed is not working in the way described anymore:

https://stackoverflow.com/a/11209320/10827339

Also, I have not set up any custom configuration for the session, I'm just using it in it's default state.

Is there any solution to this? Or, even better, is there a better way to do what I'm trying to achieve - which is effectively this process:

  1. User clicks button on /Home/Action view.
  2. Button triggers action method which does some server side processing.
  3. This process can either error or succeed, user should be informed of result.
  4. Therefore, on the same page where button was clicked, message needs to be shown that details if the process succeeded or failed, rather than user being taken to another view to be shown the result.

Would calling a Action via Ajax be a suitable solution to this problem?

Jake H
  • 155
  • 2
  • 13
  • You're redirecting to ActionMethodName when the action is called PlaceHolderActionName2? – shim-kun Jun 16 '20 at 11:05
  • @xstrafez_ edited to clear up, the names are not what I'm literally using in the application, they are what they say, placeholder names so that I could ask the question here. – Jake H Jun 16 '20 at 12:20
  • For future reference to anyone that stumbles across this question, I found out that the cause of this issue was a very particular issue to do with putting files into the `bin` folder of the application. Basically, don't write any files (like I did) to `bin` while you need the session to remain, doing so causes the AppDomain to change which causes a new session. Instead, store files in a place like `App_Data`. See this link for more: https://stackoverflow.com/questions/18337256/mvc4-session-not-persisted-between-requests – Jake H Jun 16 '20 at 12:56

0 Answers0