Introduction
In this article I explain the use of ViewBag, ViewData and TempData to pass data from a Controller to a View to render back to a client Asp.MVC.
Implementation
Launch Visual Studio then select "File" -> "New" -> "Project..." then select Visual C# or Visual Basic then select ASP.NET MVC 4 Web Application and name the application "MvcDemoApp" or whatever you want.
A "New ASP.NET MVC 4 Project "dialog box will open; select "Empty" template and leave "Razor" as the default View Engine.
Now it's time to create the controller. So in the Solution Explorer right-click on the Controller folder then select "Add" -> "Controller" then name the controller "HomeController" and leave the template "Empty MVC controller" as it is and click on the Add button.
Now in the Index action of the "HomeController" controller we will use a ViewBag to store the data that will be passed to the view to be rendered. So let's create the view by right-clicking between the Index action and select Add View then leave all the settings as they are and click on the Add button.
The following is the snapshot of the controller code, View code and the rendered output. Note: "HomeController.cs" is the controller and "Index.cshtml" is the corresponding view.
What a Viewbag is
ViewBag and ViewData are used for the same purpose, to transfer data from the Controller to the 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. About Dynamic Data Types please read my first and previous article here Dynamic Data Type in C# , in this context by dynamic we mean that we can add properties to it in the controller and read them later in the view.
What ViewData is
ViewData is a built-in dictionary object that is derived from the ViewDataDictionary class that can be accessed and set with string type key values. It is used to pass data from a Controller to a View.
The data is only alive for one request and cannot persist between requests, in other words 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.
ViewData can also be used to pass data from a Controller to a View similar to a ViewBag for the same purpose as shown above.
The following is the snapshot of the Controller code, View code and the rendered output. Note: "HomeController.cs" is the controller and "Index.cshtml" is the corresponding view.
So from the preceding two examples we saw that both the ViewBag and ViewData is used to pass the data from a Controller to a View but type casting is required when accessing the data from the ViewData.
So we need to type cast ViewData["CityList"] to List<String> ViewData["CityList"] or we can also type cast it to IEnumerable<String> ViewData["CityList"].
What TempData is
The problem with the ViewData and ViewBag for transferring data from a Controller to a View is that the data is only alive for the current request. The data is lost if a redirection occurs, in other words if one Action redirects to another action.
But when we need to persist the data between actions/redirections then ASP.NET MVC offers another dictionary object called TempData for that purpose. It is derived from TempDataDictionary and created on top of the session. It will live until the redirected view is fully loaded.
The following is how to use TempData.
- Right-click on the Models folder then seelct "Add" -> "Class" and name it "BookDetails.cs" and write the code as shown in the following image:
- Now create the controller and add the code in the controller to store the data in the TempData as shown in the following image:
- In the Index action of the "HomeController" controller we are storing the Book details in the TempData and then redirecting to the DisplayBookData that will render the Book details.
- Now create two views corresponding to Index() and DisplayBookData() actions defined in the HomeController. There is no need to write any code in the index.cshtml view because that will not render any details; instead we write the code on the DisplayBookData.cshtml view as shown in the following image:
- Now run the application. The output will be as shown in the following image: