Terminologies in MVC: Part 1 (ViewData, ViewBag, TempData)

In this article we learn about the ViewData, ViewBag, TempData in the MVC.

Hello Geeks!

This series of articles is a sub-series of my MVC article series. This series contains all the terminologies, acronyms and terms usually used in the MVC Framework. Knowledge of these is as important as knowing Model-View-Controller.

So just go through these, until I present a next article in the series "MVC for Beginners".

Getting a Theme

For accessing the previous articles, kindly go through the following articles:

Quick View

This article will provide you a brief introduction to all these three important parts of MVC view. I have included reference examples, some important points and a diagram representation of how they work.
So let's go through this.

ViewData

Data travels from the controller to the view via a ViewDataDictionary. This ViewDataDictionary is a dictionary class; we call this class "ViewData".



It is a dictionary object derived from ViewDataDictionary.



Example

ViewData["Message"] = “Say Hello to C# Corner”;
(This will give you the message "Say Hello to C# Corner".)
Or
ViewData["CurrentTime"] = DateTime.Now;
(On the other hand this will show you the current date-time of your system.)

Pin Points

  •  It is a dictionary object that is derived from ViewDataDictionary
  •  It was introduced in MVC 1.0
  •  Passes the data from the controller to the view
  •  It's a Key-Value-Collection object
  •  It requires type casting for:
  •    Checking NULL values
  •    Getting Data
  •    Avoiding Errors
  •  Proceeds until the current request (only)

ViewBag

ViewBag is just a dynamic wrapper around ViewData. With it you don't need to write the dynamic keyword, it takes the dynamic keyword internally.
We often call ViewBag "A dynamic data library".



It is a wrapper around ViewData.



Public object ViewBag { get; }

Example

ViewBag.Message = “Say Hello to C# Corner”;
(This will give you the message "Say Hello to C# Corner")
Or
ViewBag.CurrentTime = DateTime.Now;

(On the other hand this will show you the current date-time of your system.)

Pin Points

  • It is a dynamic property
  • A wrapper around ViewData
  • It was introduced in MVC 3.0
  • It is a type object
  • Doesn't require type casting
  • Proceeds until the current request (only)
  • Take advantages of C# 4.0

TempData

TempData helps in maintaining data when you move from one controller to another controller. For maintaining data it uses a session variable (internally).

It is a dictionary object that is derived from ViewDataDictionary.



Pin Points

  •  It is also a dictionary object (derived from TempDataDictionary)
  •  It's a Key-Value-Collection object
  •  It was introduced in MVC 1.0
  •  It requires typecasting for:
    • Getting Data
    • Check NULL values
    • Avoiding Errors
  • It is used to store one time messages like
    • Error Messages
    • Validation Messages
  •  It's life is very short

Complete scenario

This image represents how ViewData, ViewBag and TempData actually work with the View and Controller in MVC.


Data maintenance

This table represents the data maintenance between a controller and a view in various aspects/scenarios.