Difference Between ViewBag, ViewData and TempData in MVC
If you’re new to ASP.NET MVC, you might be wondering what these two things are and when to use each one. Because I was also not sure before went to study this.
If you’ve been using MVC and are just new to version 3 of MVC, you are probably wondering what this new ViewBag is for and if it’s different from the ViewData you’ve been using. Today I got the opportunity to explain this difference between these three MVC objects.
ViewBag and ViewData serve the same purpose in allowing developers to pass data from controllers to views. When you put objects in either one, those objects become accessible in the view. This is one way we interact between the view and the controller in ASP.NET MVC. We pass data from the view to the controller by placing it in these objects. These two objects are only for a single request. View to Controller or Controller to View.
Where in TempData is meant to be a very short-lived instance, and you should only use it during the current and the subsequent requests only!
How ViewData Works
ViewData is a dictionary of objects that are accessible using strings as keys. This means that we will write code like this:
in the Controller:
public ActionResult Index()
{
var softwareDevelopers = new List
{
"Sanjeeb",
"Girish",
"Satish"
};
ViewData["softwareDevelopers"] = softwareDevelopers;
return View();
}
In the View
<..ul>
@foreach (var developer in (List)ViewData["softwareDevelopers"])
{
<.li.> @developer <./li.>
}
<./.ul>
Note that, when we go to use out object on the view that we have to cast it since the ViewData is storing everything as object. Also, we need to be careful since we’re using magic strings to access these values.
How ViewBag Works
ViewBag uses the dynamic feature that was added in to C# 4. It allows an object to dynamically have properties added to it. The code we write using ViewBag will look like this:
In the Controller
public ActionResult Index()
{
var softwareDevelopers = new List
{
"Sanjeeb",
"Girish",
"Satish"
};
ViewBag.softwareDevelopers = softwareDevelopers;
return View();
}
In the View
<..ul>
@foreach (var developer in ViewBag.softwareDevelopers)
{
<.li.> @developer <./li.>
}
<./.ul>
Notice here that, we did not have to cast our object when using the ViewBag. This is because the dynamic we used lets us know the type. Keep in mind that these dynamics are as the name suggest, dynamic, which means that you need to be careful as these are basically magic properties instead of magic strings.
So these two things seem to work almost exactly the same. What’s the difference? The difference is only in how you access the data. ViewBag is actually just a wrapper around the ViewData object, and its whole purpose is to let you use dynamics to access the data instead of using magic strings. Some people prefer one style over the other. You can pick whichever you like. In fact, because they’re the same data just with two different ways of accessing it, you can use them interchangeably. (I don’t recommend this, but you can do it.) If you want you are able to put data into the ViewBag and access it from the ViewData or put stuff in the ViewData and access it in the ViewBag.
How TempData works
Where in TempData is meant to be a very short-lived instance, and we should only use it during the current and the subsequent requests only! Since TempData works this way, we 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 redirecting. This is because a redirect kills the current request (and sends HTTP status code 302 Object Moved to the client), then creates a new request on the server to serve the redirected view.
Once the controller redirects, the ViewBag and ViewData will contain null values. If you inspect the TempData object with debugging tools after the redirect you'll see that it is fully populated with a featured product. This is because the redirect is that only, subsequent, request, so only it can access the TempData object without worry.
So for a short answer for the interviews; These objects are two different ways of accessing the exact same data. The ViewBag is just a dynamic wrapper around the ViewData dictionary. TempData, on the other hand, is geared specifically for working with data on HTTP redirects, so remember to be cautious when using TempData.
Keep learning... :-)
Comments
http://brendan.enrick.com/post/Difference-Between-ViewBag-and-ViewData-in-MVC-3.aspx