---
title: "What is the use of ViewData in Asp.Net MVC?"  
description: "What is the use of ViewData in Asp.Net MVC?"  
author: "Shikhar Arora"  
published: 2019-12-18  
updated: 2019-12-18  
canonical: https://www.mindstick.com/forum/155595/what-is-the-use-of-viewdata-in-asp-dot-net-mvc  
category: "asp.net mvc"  
tags: [".net", "mvc"]  
reading_time: 2 minutes  

---

# What is the use of ViewData in Asp.Net MVC?

How to send [data from Controller](https://www.mindstick.com/forum/351/how-to-pass-data-from-controller-to-ascx-page) to [View](https://yourviews.mindstick.com/view/84701/layoffs-in-google-india-2023-view)

## Replies

### Reply by Shikhar Arora

ViewData is used to pass [data](https://www.mindstick.com/articles/13050/salesforce-aiming-to-dominate-predictive-analytics-with-data-science) from [controller to view](https://www.mindstick.com/forum/12918/pass-an-object-from-controller-to-view-with-asp-dot-net) and it consist of a null value when redirection occurs. ViewData is derived from ViewDataDictionary which is dictionary type and ViewData required typecasting. Following is the code scrap of ViewData in MVC

```
// Summary:
// Gets or sets the dictionary for view data.
//
// Returns:
// The dictionary for the view data.
public ViewDataDictionary ViewData { get; set; }
```

ViewData is a dictionary object that is derived from the ViewDataDictionary class. We will see how we can assign value to ViewData in Controller with a simple example for that create one new controller in the application and write code like as shown below

```
 using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;namespace Tutorial4.Controllers
{
    public class TestController : Controller
    {
        public ActionResult Index()        {
            ViewData["test"] = "hi Viewdata";
            return View();
        }
    }
}
```

Now we will create one view to show data for that right click on controller à select add view give the name for that view (Index.cshtml). Open view file and write the following code to show data in the website

```
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
@ViewData["test"]
```


---

Original Source: https://www.mindstick.com/forum/155595/what-is-the-use-of-viewdata-in-asp-dot-net-mvc

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
