I have RedirectToAction as a return value in a controller and in my view I use ViewBag. While it's a well-known problem that ViewBag becomes empty after a RedirectToAction. I have a problem when I reinitialize ViewBag values in my overloaded controller in the OnActionExecuting method. ViewBag is empty again. But the problem is that on my local PC (win7) it's working okay but on the web-hosting server (win 2008) it crashes. So can some one help saying what can cause that? Maybe it's a setting or something?
The ViewBag and ViewData only survives the current request. TempData is the thing to use when you use redirects (and only then): http://rachelappel.com/when-to-use-viewbag-viewdata-or-tempdata-in-asp.net-mvc-3-applications states this clearly:
[...] the TempData object works well in one basic scenario:
- Passing data between the current and next HTTP requests
There was an exception deep inside data layer which was incorrectly captured and thereby prevented from propagation but caused ViewBag vanishing.
So be careful with exceptions catching.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With