AfterGlow
AfterGlow

Reputation: 227

Why does the View know about the Model?

I have done some work on ASP.NET MVC 3 but I'm no expert.

So, based on the pattern definition, the view has no direct awareness of the model and does not communicate with the model directly; only controller is directly dealing with model.

However, in ASP.NET MVC 3, I can access the model data directly from the view using Razor engine. Isn't that breaking the pattern design or am I missing something?

enter image description here

Upvotes: 2

Views: 1596

Answers (4)

llwatkins
llwatkins

Reputation: 11

Refer to this article to read about the three different types of "models" introduced by ASP.NET MVC.

What it sounds like you are referring to is the view model. This model is simply a container or a way provide structure around the data that the view is responsible for displaying. It is simply a data-transfer object (does not have any behavior).

Upvotes: 0

Manas
Manas

Reputation: 2542

I guess you have mistaken the concept of MVC. MVC does not say that "View is not aware of Model". In practice, each view is tightly coupled with a model (unless view is coupled to dynamic model or base type).

MVC is actually separation of concerns.

  1. Controller decides the business rule and decides what data to be shown to user.
  2. Then it populates the Model.
  3. Then pass on the Model to View. (Think of it like passing on some data to be displayed on view).
  4. Now View is not aware how model is created and validation rules of model.
  5. Model is not aware which view will consume it. There can be many views coupled to a model.
  6. Controller is not aware of model validation or what property of model is being displayed on view.

And MVC is just a pattern to follow, it does not stop you from writing business logic inside view or make db calls from view.

Upvotes: 0

Rowan Freeman
Rowan Freeman

Reputation: 16358

the view has no direct awareness of the model and do not communicate with the model directly

Not exactly. Exactly how to interpret this statement might depend on the reader.

I've read quite a bit on and and I find similar statements scattered around and so the way it's worded can be a bit confusing.

A view knows what the model is. That is, it should know what the model type is, what the model contains or by some means know how to use the model to display what is necessary.

If we have a page called UserProfile then the view knows that it should display the user's name, email address, age and favourite website.

The view might be told to expect a UserProfileViewModel. Such a class would contain exactly the properties that the view needs so that it can easily display them.

What is better to say is that the view should not modify the model. In fact, the view should not do much of anything. Views are supposed to be stupid - they aren't for processing business rules, modifying data, connecting to databases, and so on. They just display stuff.

Upvotes: 4

devqon
devqon

Reputation: 13997

The model contains the data, the controller manipulates the data and the view displays the data (model). The view therefore is the interaction with the user.

The view must know what to show, so it has the model (with its data) to do so. It never manipulates directly, but sends information to the controller which in turn will manipulate the data.

Upvotes: 1

Related Questions