ViewBag is a dynamic container to pass the information from the Controller to View. All the data is passed from the Controller to View via a ViewDataDictionary called ViewData. ViewBag is a dynamic wrapper around ViewData, so we can access ViewData information with the dictionary for syntax and Viewbag information can be accessed using dot notation.

Controller

  1. Public ActionResult Index() {
  2. ViewData[“time”] = DateTime.Now;
  3. ViewBag.times = DateTime.Now;
  4. }

View

  1. @ViewData[“time”]
  2. @ViewBag.times