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
- Public ActionResult Index() {
- ViewData[“time”] = DateTime.Now;
- ViewBag.times = DateTime.Now;
- }
View
- @ViewData[“time”]
- @ViewBag.times