Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC: clearing TempData after a controller method executes

Tags:

asp.net-mvc

From what I've read, information placed into TempData will be there for the current request and the next request (so that you can pass information across redirects). The problem is that if I browse to a controller action that does not redirect, the information in TempData will still be there for the next request. I'm using TempData to store informational messages that are displayed to the user, so now I'm getting the messages for the first screen on the second screen also.

Is there a good time in the controller lifecycle to clear out TempData once it's not used anymore?

like image 513
Jon Kruger Avatar asked May 22 '09 16:05

Jon Kruger


1 Answers

Use ViewData instead of TempData if you are not redirecting. You should never need to clear TempData manually. If you only use it when redirecting, it will be cleared for you, automatically and at the correct time.

like image 177
Craig Stuntz Avatar answered Oct 08 '22 03:10

Craig Stuntz