A dynamic object Page can be found in System.Web.WebPages.WebPageBase, the abstract class which WebViewPage inherits from.
A dynamic object ViewBag can be found in System.Web.Mvc.WebViewPage.
Both can propagate up from a view to it’s layout page.
The other difference I can see is that ViewBag can be used in the Controller, whereas Page is only available in the view. Are there any other differences I should know about?
PageandViewBagare dynamic and they are wrapper around dictionaries.Pageis the dynamic wrapper aroundPageDatadictionary.ViewBagis the dynamic wrapper aroundViewDatadictionary.When talk about differences,
ViewBagis used to share data between controller and view or even we can use to share data between a main view and partial view. So the ViewBag can be accessed from both the controllers and views.Pageis a little different thing, it can’t be accessed from controllers and it is used to pass data between a main page and a partial page.See an example here of using
Pageto pass data between a main view and partial view.Main.cshtml
DisplayName.cshtml
Not that I’m using the
RenderPageto render the partial view also the partial view is located in the same directory where the main view is located.