ViewData / ViewBag
To pass data from Controller to View - use ViewData or ViewBag
In controller's action code:
and you use it in View as below
ViewBag is syntactical sugar for ViewData, where instead of using array-like syntax(square brackets) to add a data-key, we use object-like syntax(. notation) to the add data-key:
But ViewBag uses dynamic
keyword internally, which in turn uses reflections
internally to figure out the datatype of data at the run time and hence usually more expensive process that simply using ViewData
.
In controller's action code:
and you use it in View as below
ViewBag or ViewData can be passed only from a controller's action to view, its not passed from one action to another action.
You can redirect the control from one action to another action using the method RedirectToAction(<<actionName>>, <<controller>>)
Last updated
Was this helpful?