Updated April 6, 2023
Introduction to MVC ViewData
ViewData is used to transfer the data from the controller to the view. It contains a key-value pair, and each key should be a string. It is like a dictionary that will have a key-value pair. Basically, ASP.NET MVC offers three options that are ViewDta, ViewBag, and TempData, mainly for passing data from controller to view. Viewdata and ViewBag are both almost similar. The main similarity between ViewBag and ViewData is used to pass data from the controller to the corresponding view.
Below is a simple diagram for better understanding.
Note that ViewData transfers only Data to view from the controller, but vice – versa is not possible. In addition, it is valid only for the current request or during the current request. It means ViewData lasts only during its current request, that is, http request, and if redirection occurs, then the values of Viewdata will be erased or cleared.
Syntax of MVC ViewData
Syntax of mvc viewdata is given below:
Public ViewDataDictionaryViewData {get; set ;}
We can see that in the above code line, ViewDataDictionary is the return type of ViewData.
[DefaultMember("Item")]
Public class ViewDataDictionary :IDictionary<string, object>
We can say that it is a weakly typed dictionary object in the ViewData in ASP.NET MVC. As this is the dictionary object, it will store the data in the form of key-value pairs.
Examples of MVC ViewData
Let us create an application of MVC viewdata and understand this topic more clearly with the help of an example.
Below are the steps we need to perform.
Step 1:
Open Visual Studio and now go to File-. Click on File and new and then project. Provide a suitable name to the application and click OK. We need to select ASP.NET web application with Visual C#.
Step 2:
Select an empty templet and then click ok.
Step 3:
The application is created now, and we are having a basic MVC structure in the application. This contains 3 folders, namely Model, View, and Controller. We need to add a controller now. Right-click on a controller and add a controller.
Step 4:
Now select MVC 5 Empty Controller and click add.
Now provide a suitable name to the controller. Now click on add when you are done with the assigned name. Now we will create a controller and returning a view to the browser. This will pass the available course ViewData to the View.
Controller:
using System;
using System.Collections.Generic;
using System.Web.Mvc;
namespace ViewBagExample.Controllers
{
public class ViewBagController : Controller
{
// GET: ViewBag
public ActionResult Index()
{
List<string> Courses = new List<string>();
Courses.Add("Manual Testing");
Courses.Add("Selenium");
Courses.Add("JAVA");
Courses.Add("ETL");
ViewData["Courses"] = Courses;
return View();
}
}
}
We can see in the above example, we have added a list for the courses, which is with the key “Courses” in the ViewData Dictionary. After adding the list, now Course list will be accessed in a view as given below in the View Code. Also, don’t forget to typecast the data before use.
View code is given below:
// Index.cshtml
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<h2>Available course in EDUCba</h2>
<ul>
@{
foreach (var Courses in ViewData["Courses"] as List<string>)
{
<li> @Courses</li>
}
}
</ul>
</body>
</html>
Output:
When we run the above Project code, we will be getting the list of courses available on the EDuCba site. It is showing the available courses in the EDUCba with the list that is Manual Testing, selenium, JAVA, ETL. This list has been passed in the Courses.
Key Points of ViewData
Let us look at some Key points of ViewData.
- When we want to pass the data from Controller to View, we use ViewData.
- Data can be passed from controller to view but not vice-versa.
- ViewData allows us to store the key-value pair.it is like anObject dictionary in ASP.NET.
- It is available for the current request only.
- If the data is complex, then it requires typecasting.
- In addition, to avoid the error for complex data, we need to check the full null value.
- ViewData is faster than ViewBag.
After some key points, let us have a look on what are the similarities and differences between ViewBag and ViewData.
Similarities between ViewBag and ViewData
Some Similarities between View Bag and View Data:
- Both the View Bag and ViewData are used for the same purpose. They are used to transfer the data from the controller to the corresponding view.
- View Data and View Bag bot help to maintain the data when it is transferred.
- Both are available for the current request only.
- The value becomes Null if the redirection happens.
Difference between ViewBag and ViewData
Now let us take us to understand the difference between ViewData and ViewBag.Most of the time, people get confused between these two things.
Sr. No. | ViewData | ViewBag |
1. | It is Basically an object dictionary which is derived from ViewDataDictionary. | It is a dynamic property, which takes advantage of the new dynamic features in C# 4.0. |
2. | Introduced in MVC 1.0 and it is available above MVC 1.0 | Introduced in MVC 3.0 and available above MVC 3.0 |
3. | Type conversation code is required. | No Need to type a conversation |
4. | It requires typecasting for complex data. | It doesn’t require typecasting for complex data. |
5. | ViewData is Faster than View Bag | It is slower than the ViewData |
Recommended Articles
This is a guide to MVC ViewData. Here we also discuss the Introduction and syntax of mvc viewdata along with different examples and its code implementation. You may also have a look at the following articles to learn more –