Using ViewData in ASP.NET CORE 3.0

ViewData is Use to pass data from controller to View only and its value cannot be retained.
ViewData is derived from ViewDataDictionary which is dictionary type and ViewData required typecasting.
We are going add Controller with Name Demo4 with Index action method inside that we are going to declare ViewData with a string value.

using Microsoft.AspNetCore.Mvc;

namespace WebApplication2.Controllers
{
    public class Demo4Controller : Controller
    {
        public IActionResult Index()
        {
            ViewData["Message"] = "Hi Message";
            return View();
        }
    }
}

View

<h1>Index</h1>
Message: - @ViewData["Message"]

By Saineshwar Bageri

I am Microsoft MVP | C# Corner MVP | Code Project MVP | FULL STACK .NET Developer and working on .Net Web Technology (Asp.net, Asp.net Core,.Net Core, C#, Sqlserver, MVC, Windows, Console Application, javascript, jquery, json, ORM Dapper) and also a freelance developer.