ViewData and ViewBag in MVC

ViewData and ViewBag in MVC

 

Passing data with ViewBag.

 

The dynamic object is accessed through the Controller.ViewBag property.

 

public ViewResult Index()
{
ViewBag.Message=”Hello World”;
ViewBag.Date=DateTime.Now;
return View();
}

 

Passing Data with ViewData.

 

public ViewResult Index()
{
ViewData["Message"] = "Hello World";
ViewData["Date"] = DateTime.Now;
return View();
}

2021-10-03