---
title: "ViewData in ASP.NET MVC"  
description: "Viewdata is the collection of state that is passed to a view. A view should get all the information it needs from the viewdata. It contains key/value"  
author: "Chris Anderson"  
published: 2011-10-01  
updated: 2019-09-07  
canonical: https://www.mindstick.com/articles/756/viewdata-in-asp-dot-net-mvc  
category: "asp.net mvc"  
tags: ["asp.net mvc"]  
reading_time: 2 minutes  

---

# ViewData in ASP.NET MVC

Viewdata is the [collection](https://www.mindstick.com/articles/1718/collections-in-java) of state that is passed to a view. A view should get all the information it needs from the viewdata. It contains key/value pairs [as well as](https://www.mindstick.com/forum/156547/difference-between-max-width-and-width-as-well-as-max-height-and-height) some special [properties](https://yourviews.mindstick.com/view/81845/now-it-s-possible-to-predict-properties-of-any-molecule), such as Model. Viewdata is accessible on the [controller](https://www.mindstick.com/blog/273/passing-values-from-controller-to-view-in-asp-dot-net-mvc) as well. The controller is [responsible](https://answers.mindstick.com/qa/94149/who-is-one-of-the-responsible-person-for-the-department-of-space-dos) for filling the viewdata [dictionary](https://www.mindstick.com/articles/1500/hashtable-and-dictionary-in-c-sharp) with objects. When a view is executing, it will pull various object from viewdata while it is rendering. If an expected object is not present, then you will see the same type of [exception](https://www.mindstick.com/articles/61/exception-handling-in-c-sharp) present in any code using a dictionary collection in .Net.\

##### How to use View Data?

Here, I am going to explain how to use ViewData in View to get the information dynamically through Controller and Model.

In HomeController.cs, we set the [Current Date](https://www.mindstick.com/forum/323/find-the-day-name-and-month-name-from-current-date) and Time by using Now property of DateTime class in the field of ViewData property.

```
using System;using System.Web.Mvc;using ViewData.Models;
namespace ViewData.Controllers{
     public class HomeController :  Controller    {
         public ActionResult Index()         {
            ViewData["CurrentTime"] = DateTime.Now.ToString(); //set the current date and time in ViewData field time in ViewData field time in ViewData field  time in ViewData field
            return View(new ModelClass());         }    }
}
```

In ModelClass.cs, we created a property [named as](https://answers.mindstick.com/qa/95800/jens-stoltenberg-has-been-named-as-the-governor-of-the-central-bank-of-which-country) [FirstName](https://www.mindstick.com/interview/33973/how-to-split-the-full-name-into-firstname-and-lastname-in-sql-server) that return a name.

```
namespace ViewData.Models{     public class ModelClass    {         public string FirstName         {
            get            {                return  "Rohit";
            }
         }
    }
}
```

After setting the data in ViewData, here in View we retrieved the information in View through ViewData.

```
 <%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<ViewData.Models.ModelClass>" %>

<!DOCTYPE html>
<html>
<head runat="server">
     <title>Index</title>
</head>
<body>
     <div>
                <!--Getting data from Model ViewData -->
         <h3>Hello! <%: ViewData.Model.FirstName %> <br /><br />

              <!--Getting data from Controller View Data -->
         The current date and time is <%: ViewData["CurrentTime"] %></h3>
     </div>
</body>
</html>
```

## The above code display output like below:

![ViewData in ASP.NET MVC](https://www.mindstick.com/mindstickarticle/443c4cc1-a69c-46e4-bf18-2c27230f36bb/images/3a87a8a2-88f5-467a-b2f4-f8279e5c9cb5.png)

---

Original Source: https://www.mindstick.com/articles/756/viewdata-in-asp-dot-net-mvc

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
