Before this I would request you to go through previous articles on ASP.NET MVC
- Introduction to ASP.NET MVC
- Creating Sample MVC Application
- Passing Data from Controller to View in ASP.NET MVC 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.

Select Empty Template. Click OK.
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.
Select MVC 5 Empty Controller. Click Add.
We will give a suitable name to the controller. Click Add once name is assigned.
Let us write some code in the controller class.
Implementing ViewData
- public ActionResult Index()
- {
- ViewData["Student"] = "Nitin Tyagi";
- return View();
- }

Write the following Razor code in it.
- <div>
- <h2>UsingViewData</h2> @ViewData["Student"]
- </div>

Implementing ViewBag
Modify the Controller code as shown below.
- public ActionResult Index()
- {
- ViewBag.Name = "Nitin Tyagi";
- return View();
- }
- <div>
- <h2>Using ViewBag</h2> @ViewBag.Name
- </div>

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.
- public ActionResult Index()
- {
- TempData["Name"] = "Nitin Tyagi";
- return RedirectToAction("Index1");
- }
- public ActionResult Index1()
- {
- ViewBag.data = TempData["Name"].ToString();
- return View();
- }
- <div>
- <h2>Using TempData</h2>
- @ViewBag.data
- </div>

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.

Ravi KandelPosted Jul 14, 2016, 12:10 PM
Thanks for sharing.
Bhuvanesh MohankumarPosted May 30, 2016, 1:19 PM
Good one
Vignesh ManiPosted May 30, 2016, 9:35 AM
Nice
Raja TPosted May 30, 2016, 6:31 AM
Nice, Thanks for sharing
Thiruppathi RPosted May 30, 2016, 6:03 AM
Nice info..
Vikram ChaudharyPosted May 30, 2016, 2:01 AM
nice article..keep sharing
Humayun Kabir MamunPosted May 30, 2016, 1:54 AM
Nice...
Debasis SahaPosted May 30, 2016, 1:17 AM
Nice share..