Both ViewData and ViewBag are used to pass the data
from controller to view. ViewData is a dictionary of objects that are stored
and retrieved using strings as keys.
ViewData[“counties”]=”list of countries”; ViewBag.countries=”list of countries”;
ViewBag uses the
dynamic feature that was introduced into C# 4.It allows an object to
have properties dynamically added to it.
Both ViewData and ViewBag does not provide compile time
error checking. For example ,if you
mis-spell keys or property names, you wouldn’t get compile time error. You get
to know about the error at runtime only. Internally ViewBag properties are
stored as name/value pairs in the ViewData dictionary
To pass the data from controller to a view, It’s always
good practice to use strongly typed view models over ViewBag and ViewData.
Strongly typed View Models provide compile time error checking.
tags:viewdata in mvc 4,viewbag in mvc4 ,viewdata and viewbag ,viewbag ,viewdata in mvc,viewbag in mvc