What is the role of Controller in MVC model?

15,373

Solution 1

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

Solution 2

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.

Share:
15,373

Related videos on Youtube

Kid
Author by

Kid

Updated on June 04, 2022

Comments

  • Kid
    Kid almost 2 years

    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?

    • Stefan
      Stefan over 6 years
    • Kid
      Kid over 6 years
      @Stefan I had gone through that question. Still Confusion remained!
    • Clint
      Clint over 6 years
      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).
    • Kid
      Kid over 6 years
      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.
    • Kid
      Kid over 6 years
      @Clint So you mean business logic happens in Controller?
    • Clint
      Clint over 6 years
      @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.
    • Kid
      Kid over 6 years
      @Clint docs.microsoft.com/en-us/aspnet/core/tutorials/first-mvc-app‌​/… It clearly states business logic resides in Model.
    • Clint
      Clint over 6 years
      @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.
  • bilpor
    bilpor over 6 years
    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.
  • Camilo Terevinto
    Camilo Terevinto over 6 years
    Models should be POCOs, not holders of Business Logic. This is completely wrong.
  • Kid
    Kid over 6 years
    @CamiloTerevinto Then what is the final right answer?
  • Manikandan Sekar
    Manikandan Sekar over 6 years
    I think you misunderstand, I mentioned as controller locates the Action method, passes args to Action methods. Refer MSDN link
  • Camilo Terevinto
    Camilo Terevinto over 6 years
    @Mr.J Not really to answer here, but did you read this answer? It's a pretty good one, regardless of the mix of MVVM there.
  • Irf92
    Irf92 over 6 years
    go through this also .. codeproject.com/Articles/25057/…