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:
- User clicks button on
/Home/Action
view. - Button triggers action method which does some server side processing.
- This process can either error or succeed, user should be informed of result.
- 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?