What is the use of ViewBag, ViewData and TempData in Asp.Net MVC ?

Introduction: In this article i am going to explain what is ViewBag, ViewData and TempData and what is the difference between them and their use in Asp.Net MVC.

Description: In Asp.Net MVC (Model-View-Controller) there are three options ViewBag, ViewData, and TempData to pass small amount of data from controller to view. read the article demonstrating the Examples to pass data from controller to view

What is ViewData?

ViewData is an in-built dictionary object that is derived from ViewDataDictionary class that can be accessed and set with string type key values. It is a used to pass data from Controller to View.
The data is only alive for one request and cannot persist between requests i.e. if redirection occurs then its value becomes null. The problems with ViewData are that the type casting is required for complex data types at the location where the data is being extracted and also we need to check for null values to avoid errors.

Key points to remember about ViewData
  • ViewData is a dictionary of objects that is derived from ViewDataDictionary class and accessible using strings as keys.
  • ViewData requires typecasting for complex data type and it is recommended to check for null values to avoid error.
  • ViewData is used to pass small amount of data from controller to view, so it helps to maintain data when moving from controller to view.
  • ViewData has short life i.e.  Its value becomes null when redirection occurs because its life lies only during current request. This is because the aim of ViewData is to provide a way to transfer/pass data from controllers and views.
  • ViewData is faster than ViewBag


What is ViewBag?

ViewBag and ViewData both are used for the same purpose i.e. to transfer data from controller to view but ViewBag comes with an additional feature that the type casting for complex objects is not required. ViewBag is a dynamic property that takes advantage of the new dynamic features in C# 4.0. By dynamic we mean that we can add properties to it in the controller, and read them later in the view.

Key points to remember about ViewBag
  • ViewBag is just a wrapper around the ViewData and is used to pass small amount of data from controller to view so helps to maintain data when moving from controller to view.
  • ViewBag is dynamic object, meaning we can add properties to it in the controller, and read them later in the view.
  • ViewBag doesn’t require any type of typecasting for complex data type.
  • ViewBag also has a short life i.e.  Its value becomes null when redirection occurs because its life lies only during current request. This is because the aim of ViewBag is to provide a way to transfer/pass data from controllers and views.
  • ViewBag is slow as compared to ViewData


What is TempData?

The problem with the ViewData and ViewBag for transferring data from controller to view is that the data is only alive for current request. The data is lost if a redirection takes place i.e. if one Action redirects to another action. But when we need to persist the data between actions/redirection then asp.net MVC offers another dictionary object called TempData for that purpose. It is derived from TempDataDictionary and created on top of session. It will live till the redirected view is fully loaded.

Key points to remember about TempData
  • TempData is like ViewData, except that it persists for two successive requests thus helps to maintain data when we move from one controller to other controller or from one action to other action.
  • Typecasting for complex data type is required and it is recommended to check for null values to avoid errors.
  • TempData is uses session to store the data, behind the scenes.
  • It allows for persisting information for the duration of a single subsequent request. We store something inside TempData and then redirect. In the target controller action to which you redirected we could retrieve the value that was stored inside TempData.
  • It is mainly used to store only one time messages like error messages, validation messages etc.
  • TempData is meant to be a very short-lived instance thus lifetime of TempData is very short i.e. from the current request to the subsequent request.
  • Use TempData when you need data to be available for the next request only.

Now over to you:
" I hope you have got what is ViewBag, ViewData and TempData in Asp.Net MVC and If you like my work; you can appreciate by leaving your comments, hitting Facebook like button, following on Google+, Twitter, Linked in and Pinterest, stumbling my posts on stumble upon and subscribing for receiving free updates directly to your inbox . Stay tuned and stay connected for more technical updates."
Previous
Next Post »

If you have any question about any post, Feel free to ask.You can simply drop a comment below post or contact via Contact Us form. Your feedback and suggestions will be highly appreciated. Also try to leave comments from your account not from the anonymous account so that i can respond to you easily..