Home > DeveloperSection > Articles > ViewBag vs ViewData vs TempData in Asp.net MVC

ViewBag vs ViewData vs TempData in Asp.net MVC


ASP.Net ASP.Net 
Ratings:
0 Comment(s)
 42507  View(s)
Rate this:

ViewBag vs ViewData vs TempData in Asp.net MVC

In this article describe the concept of the viewbag, viewdata and tempdata in mvc. Here we also describes the example that we understand it.

ViewBag

The ViewBag property enables you to dynamically share values from the controller to the view. It is a dynamic object which means it has no pre-defined properties. You define the properties you want the ViewBag to have by simply adding them to the property. In the view, you retrieve those values by using same name for the property.

Some important note on ViewBag

·         ViewBag is a dynamic property that takes advantage of the new dynamic features in C# 4.0.

·         Basically it is a wrapper around the ViewData and also used to pass data from controller to corresponding view.

·         The ViewBag life also lies only during the current request.

·         If redirection occurs then viewbag value becomes null.

·         It doesn’t required typecasting for getting data.

Example

public ActionResult Index()

        {

            ViewBag.Message = "Hello MVC ViewBag property.";

            ViewBag.Name = "Amit Kumar";

            return View();

        }

 

Here we show how to use ViewBage property:

@ViewBag.Message

 

Name:@Html.TextBox("Name")

 

ViewData

ViewData is a dictionary which is derived from ViewDataDictionary.  The controller can add key/values pairs to the view data. The data is then passed to the view when the view is rendered. The view can add to or change the data, which will be sent to the controller when the view is posted as part of a request.

Some important note on ViewData

·         ViewData is a dictionary object that is derived from ViewDataDictionary class.

·         ViewData is used to pass data from controller to corresponding view.

·         Its life lies only during the current request.

·         If redirection occurs then its value becomes null.

·         It’s required typecasting for getting data and check for null values to avoid error.

Example

public ActionResult Index()

        {

 

            ViewData["Message"] = "Hello for MVC ViewData.";

 

            ViewData["Name"] = "Amit Kumar";

           

            return View();

        }

 

Here we show how to use it.

@ViewData["Message"]

 

@Html.TextBox("Name")

 

TempData

TempData is a dictionary which is derived from TempDataDictionary class. TempData is stored data just like live session for short time. TempData Keep data for the time of HTTP Request it mean that it hold data between two consecutive requests. TempData help us to transfer data between controllers or between actions. TempData internally use Session variables. Note that TempData is only work during the current and subsequent request. It is generally used to store one time message. With the help of TempData.Keep() method we can keep value in TempData object after request completion.

Some important note on TempData

·         TempData is a dictionary object that is derived from TempDataDictionary class and stored in short lives session.

·         TempData is used to pass data from current request to subsequent request (means redirecting from one page to another).

·         Its life is very short and lies only till the target view is fully loaded.

·         It’s required typecasting for getting data and check for null values to avoid error.

·         It is used to store only one time messages like error messages, validation messages.

Example

public ActionResult Index()

        {

           

            TempData["Message"] = "Hello this is my TempData program.";

 

            TempData["Name"] = "Amit Kumar";

           

            return View();

 

        }

 

Here we show how to use it.

@TempData["Message"]

       

@Html.TextBox("Name")


Don't want to miss updates? Please click the below button!

Follow MindStick