How To Use ViewData, ViewBag And TempData in MVC

Before this I would request you to go through previous articles on ASP.NET MVC

Let us first understand what is the purpose of ViewData, ViewBag and TempData in ASP.NET MVC.

ViewData

It is a dictionary which can contain key-value pairs where each key must be string. We can use it in transferring data from Controller to View. We can only transfer data from Controller to view but if we wish to pass data back again to controller then it is not possible. So it is valid only for the current request.

ViewBag

ViewBag is also similar to ViewData. It is used to transfer data from Controller to View. It is a type of Dynamic object, that means you can add new fields to viewbag dynamically and access these fields in the View. You need to initialize the object of viewbag at the time of creating new fields.

TempData

TempData is a dictionary object derived from TempDataDictionary. It is for subsequent HTTP requests; unlike ViewBag and ViewData, those stay only for current request. It can be used to maintain data between controller actions as well as redirects.

I hope the terms seem clear now. So let us create an application and implement these to understand the usage.

Open Visual Studio. Go to File->New->Project. Give a suitable name to the application. Click OK.

New

Select Empty Template. Click OK.

Template

Now our application is created. We have a very basic MVC structure in the application with primarily three folders: Model, View and Controller.

Now we will add a Controller. Right click on Controller. Add->Controller.

Controller

Select MVC 5 Empty Controller. Click Add.

Add

We will give a suitable name to the controller. Click Add once name is assigned.

Add

Let us write some code in the controller class.

Implementing ViewData

  1. public ActionResult Index()  
  2. {  
  3.     ViewData["Student"] = "Nitin Tyagi";  
  4.   
  5.     return View();  
  6. }  
Now we will add a view. Right Click on Index().

Add

Write the following Razor code in it.
  1. <div>  
  2.     <h2>UsingViewData</h2> @ViewData["Student"]  
  3.   
  4. </div>  
Let us run the application and see the output.

output
Implementing ViewBag

Modify the Controller code as shown below.
  1. public ActionResult Index()   
  2. {  
  3.     ViewBag.Name = "Nitin Tyagi";  
  4.   
  5.     return View();  
  6. }  
Modify the View as shown below.
  1. <div>  
  2.     <h2>Using ViewBag</h2> @ViewBag.Name  
  3.   
  4. </div>  
output
Implementing TempData

Write the following code in the Controller. We have created two ActionResult methods. First we would pass the control to second method and this method would pass the data to its corresponding View using ViewBag.
  1. public ActionResult Index()  
  2. {  
  3.     TempData["Name"] = "Nitin Tyagi";  
  4.   
  5.     return RedirectToAction("Index1");  
  6. }  
  7.   
  8. public ActionResult Index1()  
  9. {  
  10.   
  11.     ViewBag.data = TempData["Name"].ToString();  
  12.   
  13.     return View();  
  14.   
  15. }  
Add a new View(Index1.cshtml) and write the following code in it.
  1. <div>   
  2. <h2>Using TempData</h2>  
  3.   
  4. @ViewBag.data  
  5. </div>  
Let us run the application and check the output. Use the Student Controller and Index Action to run the application to avoid error as we are passing data from Index to Index1 method.

output
So we have just seen how tempdata maintains data between controller actions. This is the basic difference and usage of these three in ASP.NET MVC. I hope this post is useful to the developers.

 


Similar Articles