1

I have gone through some answers here and and some articles on MVC but I still have a confusion regarding the role of a Controller in a MVC application.

I read in a book that the Model is self-contained and functions independent of View and Controller. And that the Model contains the business logic and data access codes. Source

Also in the best answer here What goes into the "Controller" in "MVC"?

But the other answers around here say that it is the Controller that represent the business logic

Where can I find a dead-simple explanation of MVC?

So which is the correct answer?

pluto20010
  • 479
  • 1
  • 6
  • 15
  • 2
    Possible duplicate of [What goes into the "Controller" in "MVC"?](https://stackoverflow.com/questions/1015813/what-goes-into-the-controller-in-mvc) – Stefan Dec 19 '17 at 10:44
  • @Stefan I had gone through that question. Still Confusion remained! – pluto20010 Dec 19 '17 at 10:46
  • Voted to close simply because there's a wealth of information out there already (including here on SO); things like MVC are patterns, and not everyone follows them correctly, so you're likely to encounter different answers in different places (helpful I know!). Broadly speaking though: > Model = your data types (e.g. Customer, Order etc) > View = the actual webpage showing the information > Controller = this gathers models from the data source, does some logic and then passes it to the view (and the other way around too). – Clint Dec 19 '17 at 11:02
  • 1
    But due to that wealth I was having this genuine confusion and wanted to clear things up once and for all. Like the answers below aren't correct as well. Because of the downvote maybe I won't be able to ask further questions here. What to do>? I thought SO was supposed to clear up my understanding. – pluto20010 Dec 19 '17 at 11:23
  • @Clint So you mean business logic happens in Controller? – pluto20010 Dec 19 '17 at 12:53
  • @Mr.J I do, though typically the actual logic will be in separate classes (and form part of your business layer), and your controllers will then make use of those classes. – Clint Dec 19 '17 at 14:15
  • @Clint https://docs.microsoft.com/en-us/aspnet/core/tutorials/first-mvc-app/adding-controller It clearly states business logic resides in Model. – pluto20010 Dec 20 '17 at 06:53
  • @Mr.J like I said, it varies, and there’s a lot of debate around how to do it “correctly”. An advantage to having logic in the model classes is that you can reuse it outside the controller. But in that case, use things like repositories and other classes for that. – Clint Dec 20 '17 at 09:05

4 Answers4

2

Wikipedia states very simply: The controller, accepts input and converts it to commands for the model or view.

https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller

1

This answer is correct

Model: This component represents the data and the business logic of the application.The model in the MVC framework is not related to the presentation of the application.The model component focus on keeping track of the state of the application.It also defines business rules for data, means how the data can be changed and manipulated.

View: The view provides the user interface (UI) for the model. The main work (function) of the view represents the information in user understandable format. It uses UI Components such as HTML,CSS,Jquery etc.

Controller: Controller act as a mediator between view and model. it is responsible to control the data transmission between the model and the view. It maps the user action into model updates.The controller layer is helpful to select the most appropriate view and delivers it to the user.

Actually, The Controllers are mediater between the view and model. they don't define the business logic. Models are responsible for business logic.

Irf92
  • 240
  • 1
  • 12
  • 1
    The Model should not be responsible for business Logic, they are responsible for your object structures. If it is you have just broken the first rule of Solid. Your business Logic should be defined in a separate layer. So that should you wish to change to WPF for instance you can still make referecnce to your business layer. – bilpor Dec 19 '17 at 11:01
  • 2
    Models should be POCOs, not holders of Business Logic. This is completely wrong. – Camilo Terevinto Dec 19 '17 at 11:12
  • 1
    @CamiloTerevinto Then what is the final right answer? – pluto20010 Dec 19 '17 at 11:31
  • @Mr.J Not really to answer here, but did you read [this answer](https://stackoverflow.com/a/3540895/2141621)? It's a pretty good one, regardless of the mix of MVVM there. – Camilo Terevinto Dec 19 '17 at 11:37
  • go through this also .. https://www.codeproject.com/Articles/25057/Simple-Example-of-MVC-Model-View-Controller-Design – Irf92 Dec 19 '17 at 15:43
0

Controller acts as a mediator between the View and the Model class. It is used just to remove the dependency between model and view classes. Kindly refer to this link.. https://www.tutorialspoint.com/mvc_framework/mvc_framework_controllers.html

-1

The ASP.NET MVC framework maps URLs to classes that are referred to as controllers. Controllers process incoming requests, handle user input and interactions, and execute appropriate application logic. A controller class typically calls a separate view component to generate the HTML markup for the request.

The Controller class is responsible for the following processing stages:

  • Locating the appropriate action method to call and validating that it can be called.
  • Getting the values to use as the action method's arguments.
  • Handling all errors that might occur during the execution of the action method.
  • Providing the default WebFormViewEngine class for rendering ASP.NET page types (views).
  • I think you misunderstand, I mentioned as controller locates the Action method, passes args to Action methods. Refer [MSDN link](https://msdn.microsoft.com/en-us/library/dd410269(v=vs.98).aspx) – Manikandan Sekar Dec 19 '17 at 11:33