A Beginner’s Tutorial on Various Ways of Passing Data in ASP.NET MVC Application

In this article we will discuss about few important data transfer techniques in ASP.NET MVC i.e. ViewData, ViewBag, TempDataand the good old Sessionvariables. We will try to see which should be used when and what are the advantages and disadvantages of one over the other.

Background

In one of our previous articles we talked about the basic of MVC and what benefits MVC has to offer (refer this: An Absolute Beginner’s Tutorial on ASP.NET MVC for Web Forms Developers[^]). In this article we will talk about how we can transfer the data from Controller to Viewor passing data between actions of a controller.

Model

The best and recommended way to pass the data from a Controllerto Viewis by using Modelclasses. If we create a strongly typed view than that view is capable of accessing the object/model that is being passed to it from the controller. So for most cases where we need to use some data we should create strongly typed views and pass on Modelobjects to the views.

Now when we talk about Modelclasses there are three main type of Model classes. First are the entities that represent our data model. Second are the classes that represent the domain model of our application. Now from an application perspective we will mostly be using these domain models in our view so that the application data can be extracted and shown in the views. We could also use the entities directly if there is a need to show the data from data model directly.

ViewModel

Now consider a scenario where we need to create a view which needs data from two or more classes/models. How can we create a strongly type view that can extract data from two models. The answer to this is that we cannot. But we can create a new class that can contain these classes’ objects in it and this class can be passed as the model class to the view. Now this class is created to be used as model on some views. All such classes are called as ViewModels.

ViewData and ViewBag

We saw how Models and ViewModelscan be used to pass data from controller to the view. But now consider scenario where we need to pass small amount of data from controllers to view. Now for every such case if we start creating ViewModelsthen managing these view models will become a nightmare. For all such requirements where we need to transfer small amount of data, we can use Viewdataand ViewBag.

ViewData

ViewDatais an in built dictionary object that can be accessed and set with string type key values. It is derived from ViewDataDictionaryclass and is a very handy data structure when it comes to passing data from Controller to View. The data is only alive for one request and cannot persist between request. The only problem with ViewDatais that the type casting is required for complex data types at the location where the data is being extracted.

ViewBag

C# 4.0 has the possibility of having dynamic Variables. ViewBagdata structure has been created to utilize this and is written as a wrapper over the ViewDataso that the type casting for complex objects will not be required if we use ViewBag. ViewBagcomes with all the benefits of ViewDatawith an additional benefit that type casting is not required.

TempData

Now the problem with all the above ways of data transfer was that the data is only alive for current request. The data is lost if a redirection takes place i.e. one Action redirects to another action. For the scenarios where we need to persist the data between actions/redirection another dictionary object called TempDatacan be used. It is derived from TempDataDictionary. It is created on top of session. Its will live till the redirected view is fully loaded.

Sessions

Sessionis the way to persist the data till the current session is alive. If we need some data to be accessible from multiple controllers, actions and views then Session is the way to store and retrieve the data.

Using the code

Let us now create a simple MVC 3.0 web application with razorviews to see all the above mentioned data transfer methodologies in action.

Model

Let us first see how we can create a strongly typed view which will display the data that is being passed to it from the controller. Lets have a simple Model like:

Now let us create a simple Action called SampleBookand create a book object in it and then pass it on to the view.

Now lets add a strongly typed View for this action which will use Bookmodel to retrieve the data. Lets also use the “ Details” scaffold so that we don’t have to write the code to access the data from the model(just for the demonstration purpose).

modelview

Now if we look at the code for the View we can see that the data is being extracted from the Modelwhich is an object of Booktype passed from the controller.

ModelViewCode

When we run the application:

modelviewScreenshot

ViewModel

Now let us say we need a View that should display the data from multiple models. Lets say we have one more class which is getting some custom message and it should be displayed on the page whenever a Bookdata is being displayed. Now this message data is contained in a separate model as:

Now we need to pass two objects to the View one Book object and another Message object. Now to do this we need to create a new class i.e. the ViewModelclass which will contain these two object in it.

Now let is create one more action in our controller. This action will create this ViewModelobject and pass it to the view.

Now let us create a strongly typed view that will use this ViewModelclass to extract the data.

viewmodelview

And now let us see the code that we need to extract the data from this ViewModeland show it on the View.

viewModelCode

And when we run the application we can see that data can be extracted from this ViewModelclass and then corresponding data from Bookand Messageclass and be extracted and shown on the View.

viewModelScreenshot

ViewData

We have discussed that the ViewDatacan be used to pass simple and small data from controller to the view Let is see how we can pass a simple message string from Controller to View using ViewData.

And now this data can be extracted from the view as:

viewdataCode

And when we run the application:

viewDataScreenshot

Note: We are not type casting the data in view because it is simple string. If this data would have been of some complex type, type casting would become inevitable.

ViewBag

Now let us try to implement the same functionality like above but by using ViewBaginstead of ViewData.

And now this data can be extracted from the view as:

viewbagCode

And when we run the application:

viewbagScreenShot

TempData

Now let us say we have an action method redirect to another action method and we need to pass some data from one action method to another action method. To do this you we will have to use TempData.

Now when the SampleBook3action will be called it will create a Book type, put it in a TempDatavariable and then redirect to action SampleBook4. Now in SampleBook4, the data will be extracted from the TempDataand then the View for the SampleBook4will be shown which is a strongly typed view and is capable of showing the data from the Book model.

Session

Use of sessions has nothing new for the the web developers. We can use session variables to persist the data for a complete session. To demonstrate this let us implement the above functionality using sessions instead of TempData.

Now when the SampleBook5action will be called it will create a Book type, put it in a session variable and then redirect to action SampleBook6. Now in SampleBook6, the data will be extracted from the Session and then the View for the SampleBook6will be shown which is a strongly typed view and is capable of showing the data from the Book model.

Note: The sample application contains complete application demonstrating all the techniques. Please refer to the sample application for better understanding.

Point of interest

In this article we saw various ways of passing the data between controllers and view and between action in a controller. This article has been written from an MVC beginner’s perspective. I hope this has been informative.

Download sample code for this article: PassingDataSample