ViewBag vs ViewData
- ViewData is a dictionary of objects that is derived from the ViewDataDictionary class and accessible using strings as keys.
- ViewBag is a dynamic property that takes advantage of the new dynamic features in C# 4.0.
- ViewData requires typecasting for complex data types and checks for null values to avoid errors.
- ViewBag doesn't require typecasting for complex data types.
- Calling of ViewBag is
C#
ViewBag.Name = "Yogesh"; - Calling of ViewData is
C#
ViewData["Name"] = "yogesh";

Join the conversation! Your thoughts help the community grow.