8.24.2012

Difference between ViewData,ViewBag and TempData in MVC 3.0

Difference between ViewData,ViewBag and TempData in MVC 3.0
ViewData ViewBag TempData
ViewData is a dictionary of objects that are accessible using strings as keys
ViewBag uses the dynamic feature that was added in to C# 4.0 It allows an object to dynamically have properties added to it. We can say ViewBag=ViewData + dynamic wrapper around the ViewData dictionary.
Temp data use during the current and subsequent request only means it is use when you are sure that next request will be redirecting to next view.

Example for ViewData:
ViewData[“JustLearn”]=”Just Learn on http://www.asp.net/ is rock!!”

Example for ViewBag:
ViewBag.JustLearn =”Just Learn on http://www.asp.net/ is rock!!”

Example for TempData:
TempData[“JustLearn”]=”Just Learn on http://www.asp.net/ is rock!!”

1 comment: