Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between @Model and @ViewData.Model?

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?

like image 363
cameron Avatar asked Mar 23 '13 12:03

cameron


People also ask

What is the difference between TempData ViewData and ViewBag?

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.

What is the purpose of ViewData?

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.

What is ViewData in razor?

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.

How do you define ViewData?

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.


1 Answers

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; }
    }

    //...
}
like image 143
nemesv Avatar answered Oct 05 '22 08:10

nemesv