Difference Between ViewBag & ViewData in MVC

ViewBag vs ViewData 

  1. ViewData is a dictionary of objects that is derived from the 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 types and checks for null values to avoid errors.
  4. ViewBag doesn't require typecasting for complex data types.
  5. Calling of ViewBag is
    ViewBag.Name = "Yogesh";
    
  6. Calling of ViewData is
    ViewData["Name"] = "yogesh";
    
Next Recommended Reading ViewData VS ViewBag Vs TempData in MVC