In MVC, when we want to transfer the data from the controller to view, we use ViewData. It is a dictionary type that stores the data internally. ViewData contains key-value pairs which means each key must be a string in a dictionary. The only limitation of ViewData is, it can transfer data from controller to view.

ViewData in ASP.NET Core MVC Application
In this Video, I am going to discuss the use of ViewData in ASP.NET Core MVC application with examples. Please read our previous Video before proceeding to this Video where we discussed application.
In ASP.NET Core MVC application, we can pass the data from a controller action method to a view in many different ways such as by using ViewBag,ViewData, and using a strongely typed model. In this Video, I will show you how to use ViewData to pass the data from the controller action method to a view. The rest techniques i.e. going to be discussed in our upcoming Videos.
What is ViewData in ASP.NET Core MVC Application?
The ViewData in ASP.NET Core MVC is a dictionary of weakly typed objects which is derived from the ViewDataDictionary class.
Now, look at the definition of ViewDataDictionary as shown below.
As you can see in the above image, the ViewDataDictionary implements ICollection, IEnumerable, and IDictionary interfaces where it will store the data in the form of key-value pairs. Here, the key is of type String and the value is of type object i.e. we can store any type of data.
What is the use of ViewData?
- ViewData is used to pass the data from the controller action method to a view and we can display this data on the view.
- The ViewData is work on the principle of Key-value pairs. This type of binding is known as loosely binding.
- We can pass any type of data in ViewData like normal integer, string, even though you can pass objects.
- ViewData uses the ViewDataDictionary type.
How to use ViewData?
First, we need to create a new key in ViewData and then assign some data to it. The key should be in string format and you can give any name to it and then you can assign any data to this key.
ViewData[“KeyName”] = “Some Data”;
Since ViewData is a server-side code, hence to use it on view, we need to use the razor syntax i.e. @
@ViewData[“KeyName”]
You can access the string data from the ViewData dictionary without casting the data to string type. But if you are accessing data other than the string type then you need to explicitly cast the data to the type you are expecting.
Example: Accessing string data
Example: Accessing Student Data:
Note: We can use n number of ViewData on a single view. ViewData can also be used to pass data from view to its layout view.
ViewData Example in ASP.NET Core MVC Application:
Let us see an example to understand how to use ViewData to pass data from a controller action method to a view. In our example, we want to pass three pieces of information to the view from the controller action method. One is the Title of the page, the second is the Header of the Page and the third one is the Student data that we want to display on the page.
First, create an ASP.NET Core Web Application with MVC (Model-View-Controller) project template. Once you create the project then add a class file with the name Student.cs in the Models folder. And then copy and paste the below code in it.
Modifying the HomeController:
Now, modify the Home Controller class as shown below. Here, we are removing the existing code and adding one action method i.e. Details.
Creating Details.cshtml view:
In our previous Video, we discussed the different ways to create Views in ASP.NET Core Application. Let us add a view with the name Details.cshtml within the Home Folder which is present inside the View Folder as shown below.
Now open Details.cshtml and then copy and paste the below code in it. As you can see in the below code, we directly access the string data from the ViewData without typecasting. But while accessing the Student data from the ViewData, we are typecasting it to the appropriate type.
Now run the application and navigate to the “/Home/Details” URL and you will see the data as expected as shown below.
Points to Remember:
The ViewData is dynamically resolved at runtime, as a result, it does not provide any compiles time error checking as well as we do not get any intelligence. For example, if we miss-spell the key names then we wouldnt get any compile-time error. We get to know about the error only at runtime.
The ViewData only transfers the data from the controller action method to a view, but not vice-versa. That means it is valid only during the current request.