In my view page, both seem work. But for @Model
, visual studio tells me it's WebViewPage<T>.Model
, and for @ViewData.Model
, it is ViewDataDictionary<T>.Model
. So what's the real difference there?
To summarize, ViewBag and ViewData are used to pass the data from Controller action to View and TempData is used to pass the data from action to another action or one Controller to another Controller.
In MVC, when we want to transfer the data from the controller to view, we use ViewData. It is a dictionary type that stores the data internally. ViewData contains key-value pairs which means each key must be a string in a dictionary.
ViewData is a container for data to be passed from the PageModel to the content page. ViewData is a dictionary of objects with a string-based key. You add items to ViewData as follows: public class IndexModel : PageModel.
ViewData is a dictionary of objects that are stored and retrieved using strings as keys. It is used to transfer data from Controller to View. Since ViewData is a dictionary, it contains key-value pairs where each key must be a string. ViewData only transfers data from controller to view, not vice-versa.
No there is no difference. In fact WebViewPage<T>.Model
just calls ViewData.Model
.
You can check the implementation on codeplex:
public abstract class WebViewPage<TModel> : WebViewPage
{
//...
public new TModel Model
{
get { return ViewData.Model; }
}
//...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With