---
title: "Define ViewData in MVC."  
description: "Define ViewData in MVC."  
author: "Anonymous User"  
published: 2020-01-27  
updated: 2020-01-27  
canonical: https://www.mindstick.com/forum/155740/define-viewdata-in-mvc  
category: "asp.net mvc"  
tags: ["asp.net mvc"]  
reading_time: 2 minutes  

---

# Define ViewData in MVC.

[please tell me](https://www.mindstick.com/forum/33900/please-tell-me-what-are-the-technique-to-content-optimization) about [viewdata](https://www.mindstick.com/forum/12798/there-is-no-viewdata-item-of-type-ienumerable-selectlistitem-that-has-the-key-username) in [MVC](https://www.mindstick.com/forum/155803/define-cache-profile-in-mvc) with suitable example.

## Replies

### Reply by Mike Jason

Like science fiction, diverse worlds of the future?.. like gloomy cyberpunk, neo-modern utopia, then take a look at this blog - SCI-fi arts. There you will find many new beautiful science fiction artworks from the most famous authors.\

### Reply by Nishi Tiwari

## ViewData

ViewData is defined as a dictionary of objects and derived from **ViewDataDictionary class.** We can access data value by using string as a key. It is the **[type-safe and requires typecasting for the data type](https://www.mindstick.com/forum/155739/define-viewbag-in-mvc)**. It also avoids error and check for null reference at run time. It is mainly accessible only during current request.

## Example

We are creating the controller and returning a view to the browser. This controller passes ViewData to the view.

## Controller

```
1.	using System;
2.	using System.Collections.Generic;
3.	using System.Web.Mvc;
4.	namespace ViewBagExample.Controllers
5.	{
6.	    public class ViewBagController : Controller
7.	    {
8.	        // GET: ViewBag
9.	        public ActionResult Index()
10.	        {
11.	            List<string> Courses = new List<string>();
12.	            Courses.Add("J2SE");
13.	            Courses.Add("J2EE");
14.	            Courses.Add("Spring");
15.	            Courses.Add("Hibernates");
16.	            ViewData["Courses"] = Courses;
17.	            return View();
18.	        }
19.	    }
20.	}
```

## View

```
// Index.cshtml
1.	<!DOCTYPE html>
2.	<html>
3.	<head>
4.	    <meta name="viewport" content="width=device-width" />
5.	    <title>Index</title>
6.	</head>
7.	<body>
8.	    <h2>List of Courses</h2>
9.	    <ul>
10.	        @{
11.	            foreach (var Courses in ViewData["Courses"] as List<string>)
12.	            {
13.	                <li> @Courses</li>
14.	            }
15.	        }
16.	    </ul>
17.	</body>
18.	</html>
```

Output:

It produces the following output to the browser.

![Define ViewData in MVC.](https://www.mindstick.com/mindstickforums/c0f8fc33-1375-49e2-8117-0fea3178334e/images/18d33b03-f5a1-4430-abd3-1d45dceacf3d.png)\


---

Original Source: https://www.mindstick.com/forum/155740/define-viewdata-in-mvc

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
