Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What is ViewData in ASP .Net MVC C#?
ViewData is a dictionary-based container in ASP.NET MVC that enables data transfer from Controller to View. It stores key-value pairs where keys are strings and values are objects, making it a flexible but loosely-typed data transfer mechanism.
ViewData is valid only during the current HTTP request and provides one-way communication from controller to view. Since it uses string keys and object values, it requires explicit casting when retrieving data and does not provide compile-time type checking.
Syntax
Following is the syntax for storing data in ViewData −
ViewData["keyName"] = value;
Following is the syntax for retrieving data from ViewData −
Type variable = (Type)ViewData["keyName"];
Key Characteristics of ViewData
-
Dictionary-based: Uses string keys to store and retrieve object values
-
Loosely-typed: Requires explicit casting when retrieving data
-
Request-scoped: Data is available only during the current HTTP request
-
One-way transfer: Transfers data from Controller to View only
-
No compile-time checking: Misspelled keys cause runtime errors, not compile-time errors
Using ViewData in Controller
Example
using System;
using System.Collections.Generic;
using System.Web.Mvc;
namespace DemoMvcApplication.Controllers {
public class HomeController : Controller {
public ViewResult Index() {
ViewData["Countries"] = new List<string> {
"India",
"Malaysia",
"Dubai",
"USA",
"UK"
};
ViewData["Title"] = "Countries List";
ViewData["Count"] = 5;
return View();
}
}
}
Using ViewData in View
Example
@{
ViewBag.Title = "Countries List";
}
<h2>@ViewData["Title"]</h2>
<p>Total Countries: @ViewData["Count"]</p>
<ul>
@foreach(string country in (List<string>)ViewData["Countries"]) {
<li>@country</li>
}
</ul>
Output
Countries List Total Countries: 5 ? India ? Malaysia ? Dubai ? USA ? UK
ViewData vs ViewBag vs TempData
| Feature | ViewData | ViewBag | TempData |
|---|---|---|---|
| Type | Dictionary (ViewDataDictionary) | Dynamic object | Dictionary (TempDataDictionary) |
| Syntax | ViewData["key"] | ViewBag.PropertyName | TempData["key"] |
| Compile-time checking | No | No | No |
| Casting required | Yes | Sometimes | Yes |
| Lifespan | Current request only | Current request only | Current and next request |
Common Use Cases
-
Passing simple data: Strings, numbers, or basic objects to views
-
Page metadata: Titles, descriptions, or configuration values
-
Collection data: Lists or arrays for dropdown menus or tables
-
Conditional rendering: Flags to control view behavior
Conclusion
ViewData provides a simple dictionary-based mechanism for passing data from Controller to View in ASP.NET MVC. While it offers flexibility, it lacks compile-time type checking and requires explicit casting, making ViewBag or strongly-typed models often preferable for complex scenarios.
