Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.net MVC - how does the view access the model?

In all MVC Diagrams i've seen, there is always a connection between the View and the Model, indicating that the View has access to the Model.

I just wonder: When does this apply? At the moment, I have my Controller Action taking in a Parameter from the QueryString, query the Model in order to get MyObjectViewData, and then return a View, passing in the MyObjectViewData. So essentially I have the Controller in between.

This seems to be the logical approach, but it does not fit the assumption that Model and View have business together.

What are typical situations where they interact without the Controller?

like image 349
Michael Stum Avatar asked May 03 '09 12:05

Michael Stum


People also ask

How do models connect to view?

Go to solution explorer => Views Folder => Right-click on “Model” Folder >> go to “Add” >> Click on [Class] as follow. Provide the required name like “Product. cs” then click on “Add” button as follow.

What does the view do in MVC?

What Does View (MVC) Mean? A View, in the context of a Model View Controller (MVC) architecture, is a software class that contains a template and data form and produces a response for the browser. It receives data from the Controller of the MVC and packages it and presents it to the browser for display.

How do you pass model data into a view?

The other way of passing the data from Controller to View can be by passing an object of the model class to the View. Erase the code of ViewData and pass the object of model class in return view. Import the binding object of model class at the top of Index View and access the properties by @Model.

How model view and controller interact with each other?

First, the browser sends a request to the Controller. Then, the Controller interacts with the Model to send and receive data. The Controller then interacts with the View to render the data. The View is only concerned about how to present the information and not the final presentation.


1 Answers

In MVC (on the web) the View directly interacts with the Model by rendering a particular instance of a Model entity. It only indirectly acts on the Model via the Controller. In a non-web implementation you can use the observer pattern to register handlers with View elements that may invoke methods in the Model that update the Model without Controller interaction. ASP.NET MVC and other web-based implementations are more loosely coupled than non-web implementations. In my opinion, this is a good thing. It does tend to make for a much fatter Controller implementation and you need to be careful to keep the separation of concerns between the Controller and Model.

Web implementations probably ought to be drawn without the indirect connection from the Model to the View since you can't really communicate between the two without the Controller. I suppose one could argue that a web service could provide this connection, but I would consider it to be just another form of a Controller.

like image 95
tvanfosson Avatar answered Oct 04 '22 20:10

tvanfosson