ASP.NET Core Tutorialprovides basic and advanced concepts of C# for beginners and professionals.

ASP.NET Core MVC – ViewData

Back to: ASP.NET Core Tutorial

ViewData

Type: ViewDataDictionary (key-value pair, loosely typed)

Scope: From Controller to View

✅ Use Case:

Passing small amounts of temporary data from controller to view.

✅ Example:

Controller:


public IActionResult Index() { ViewData["Message"] = "Hello from ViewData"; return View(); }

View (Razor):


html
<h2>@ViewData["Message"]</h2>

⚠️ Considerations:

  • Requires type casting

  • Null reference risk

  • Not strongly typed

Scroll to Top