Home » ASP » ASP.MVC » Differences between ViewData, ViewBag, Session and TempData

Differences between ViewData, ViewBag, Session and TempData

These three mechanisms transport information from one page to the other. ViewData and View bag can be threat in the same group because they are both the same thing. The ViewBag is an object that use ViewData. The main difference is that it’s possible to access the values from dynamic properties instead of accessing from an array with a string key.

        ViewData["Name"] = "Patrick";
        ViewBag.Name = "Patrick";

So, in fact, we are storing an object value into a dictionary which is defined to have Object type value and string key.

TempData is also a dictionary of string key and object value. The difference is that the life cycle of the object. TempData keep the information for the time of an HTTP Request. This mean only from one page to another. This also work with a 302/303 redirection because it’s in the same HTTP Request.

      TempData["Name"] = "Patrick"; 

The ViewData and ViewBag won’t have any value after a redirection. This is because their goal is to provide a way to communicate between controllers and views. It’s a communication mechanism within the server call. The life cycle of the ViewData and ViewBag is shorter than TempData.

The last mechanism is the Session which work like the ViewData, like a Dictionary that take a string for key and object for value. This one is stored into the client Cookie and can be used for a much more long time. It also need more verification to never have any confidential information.

If you like my article, think to buy my annual book, professionally edited by a proofreader. directly from me or on Amazon. I also wrote a TypeScript book called Holistic TypeScript

6 Responses so far.

  1. Andrew says:

    Nice explanation – thank you!

  2. mumio says:

    I wanted to create you that very little word in order to give thanks as before for the amazing methods you’ve featured here. It was simply tremendously open-handed of you to supply without restraint just what a number of us would have marketed as an e book to help with making some dough for their own end, certainly now that you could possibly have tried it in the event you decided. Those pointers likewise worked like a great way to fully grasp that many people have the identical passion really like my own to realize very much more regarding this condition. I’m sure there are several more pleasurable instances up front for individuals that read through your blog post.

  3. Itupoker says:

    This is the suitable weblog for anybody who needs to search out out about this topic. You realize so much its nearly onerous to argue with you (not that I really would want…HaHa). You definitely put a new spin on a topic thats been written about for years. Great stuff, simply nice!

  4. y a pas mieux pour trouver des bonnes infos

  5. Generally I don’t read post on blogs, but I wish to say that this write-up very forced me to take a look at and do so! Your writing style has been surprised me. Thanks, very nice post.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.