Difference Between ViewBag & ViewData in MVC

ViewBag vs ViewData in MVC ? Difference between ViewBag & ViewData?

  1. ViewData is a dictionary of objects that is derived from ViewDataDictionary class and accessible using strings as keys.
  2. ViewBag is a dynamic property that takes advantage of the new dynamic features in C# 4.0.
  3. ViewData requires typecasting for complex data type and check for null values to avoid error.
  4. ViewBag doesn't require typecasting for complex data type.
  5. Calling of ViewBag is :

    ViewBag.Name = "Yogesh";

    Calling of ViewData is :

    ViewData["Name"] = "yogesh";

Ebook Download
View all
Learn
View all