Thursday, August 11, 2011
ViewBag, ViewData, or TempData in ASP.NET MVC
- ViewData is a dictionary object that you put data into, which then becomes available to the view. ViewData is a derivative of the ViewDataDictionary class, so you can access by the familiar "key/value" syntax.
- The ViewBag object is a wrapper around the ViewData object that allows you to create dynamic properties for the ViewBag.
TempData
TempData is meant to be a very short-lived instance, and you should only use it during the current and the subsequent requests only! Since TempData works this way, you need to know for sure what the next request will be, and redirecting to another view is the only time you can guarantee this. Therefore, the only scenario where using TempData will reliably work is when you are redirectinghttp://rachelappel.com/when-to-use-viewbag-viewdata-or-tempdata-in-asp.net-mvc-3-applications
Subscribe to:
Posts (Atom)