What is the difference between ViewBag and ViewData in MVC?

Technology CommunityCategory: ASP.NET MVCWhat is the difference between ViewBag and ViewData in MVC?
VietMX Staff asked 3 years ago

ViewBag is a wrapper around ViewData, which allows to create dynamic properties. Advantage of viewbag over viewdata will be:

  • In ViewBag no need to typecast the objects as in ViewData.
  • ViewBag will take advantage of dynamic keyword which is introduced in version 4.0. But before using ViewBag we have to keep in mind that ViewBag is slower than ViewData.