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.
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();
- }
Now we will add a view. Right Click on Index().
Write the following Razor code in it.
- <div>
- <h2>UsingViewData</h2> @ViewData["Student"]
-
- </div>
Let us run the application and see the output.
Implementing ViewBag
Modify the Controller code as shown below.
- public ActionResult Index()
- {
- ViewBag.Name = "Nitin Tyagi";
-
- return View();
- }
Modify the View as shown below.
- <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();
-
- }
Add a new View(Index1.cshtml) and write the following code in it.
- <div>
- <h2>Using TempData</h2>
-
- @ViewBag.data
- </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.
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.