How the Model View Controller Architecture Works – MVC Explained (2024)

/ #Software Architecture
How the Model View Controller Architecture Works – MVC Explained (1)
Nishant Kumar
How the Model View Controller Architecture Works – MVC Explained (2)

Over the last 20 years, websites have changed from simple pages with a little CSS to become much more complex and powerful applications.

To make these applications easier to develop, programmers use different patterns and software architectures to make the code less complicated.

But first, what is software architecture?

An architecture is a systematic way in which software is described. It also refers to its relationship with other software, and how they interact with each other.

Software architecture also includes other factors such as business strategy, quality attributes, human dynamics, design, and the IT environment.

In other words, an architecture serves as a blueprint for a system.

Model-View-Controller (MVC) Architecture

The most popular software architecture, by far, is the Model-View-Controller, or MVC.

MVC divides any large application into three parts:

  1. The Model
  2. The View
  3. The Controller

Each of these components is built to handle a specific aspect of an application and has different purposes.

The Model

The model contains all the data-related logic that the user works with, like the schemas and interfaces of a project, the databases, and their fields.

For example, a customer object will retrieve the customer information from the database, manipulate or update their record in the database, or use it to render data.

The View

The view contains the UI and the presentation of an application.

For example, the customer view will include all the UI components such as text boxes, dropdowns, and other things that the user interacts with.

The Controller

And finally, the controller contains all the business-related logic and handles incoming requests. It is the interface between the Model and the View.

For example, the customer controller will handle all the interactions and inputs from the customer view and update the database using the customer model. The same controller will be used to view the customer data.

Here's a diagram to help visualize the MVC architecture, and how everything works together:

How the Model View Controller Architecture Works – MVC Explained (3)

How MVC Architecture works

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. It will be a dynamic HTML file that renders data based on what the Controller sends it.

Finally, the View will send its final presentation to the Controller and the Controller will send that final data to the user output.

The important thing is that the View and the Model never interact with each other. The only interaction that takes place between them is through the Controller.

This means the logic of the application and the interface never interacts with each other, which makes writing complex applications easier.

Let’s look at a simple example:

How the Model View Controller Architecture Works – MVC Explained (4)

Let's see what's going on here. First, a user inputs that they want a list of movies through a web browser or a mobile application.

The browser will then send the request to the Controller to get the list of movies.

Next, the Controller will ask the Model to find the list of movies from the database.

router.get('/',ensureAuth, async (req,res)=>{ try{ const movies = await Movies.find() (*) res.render('movies/index',{ movies }) } catch(err){ console.error(err) res.render('error/500') } }) 

Then the Model searches the database and returns the list of movies to the Controller.

const mongoose = require('mongoose') const MovieSchema = new mongoose.Schema({ name:{ type:String, required:true }, description:{ type:String } }) module.exports = mongoose.model('Movies',MovieSchema)

If the Controller gets the list of movies from the Model, the Controller will ask the View to present the list of movies.

router.get('/',ensureAuth, async (req,res)=>{ try{ const movies = await Movies.find() res.render('movies/index', { movies (*) }) } catch(err){ console.error(err) res.render('error/500') } })

‌Then the View will receive the request and returns the rendered list of movies to the Controller in HTML.

<div class="col" style="margin-top:20px;padding-bottom:20px"> <div class="ui fluid card"> <div class="content"> <div class="header">{{movie.title}}</div> </div> <div class="extra content"> <a href="/movies/{{movie._id}}" class="ui blue button"> More from {{movie.description}} </a> </div> </div></div>

Lastly, the Controller will take that HTML and return it back to the user, thus getting the list of Movies as the output.

Wrapping Up

There are a lot of software architectures out there, but Model-View-Controller is the most popular and widely used. It reduces the code complexity and makes the software easily understandable.

Now you know the concepts behind the Model-View-Controller.

That’s all folks! Happy Learning!

ADVERTIsem*nT

ADVERTIsem*nT

ADVERTIsem*nT

ADVERTIsem*nT

How the Model View Controller Architecture Works – MVC Explained (5)
Nishant Kumar

I build projects to learn how code works.And while I am not coding, I enjoy writing poetry and stories, playing the piano, and cooking delicious meals.

If you read this far, thank the author to show them you care.

Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started

ADVERTIsem*nT

How the Model View Controller Architecture Works – MVC Explained (2024)

FAQs

How the Model View Controller Architecture Works – MVC Explained? ›

-MVC is an architectural pattern

architectural pattern
An architectural pattern is a general, reusable resolution to a commonly occurring problem in software architecture within a given context. The architectural patterns address various issues in software engineering, such as computer hardware performance limitations, high availability and minimization of a business risk.
https://en.wikipedia.org › wiki › Architectural_pattern
consisting of three parts: Model, View, Controller. Model: Handles data logic. View: It displays the information from the model to the user. Controller: It controls the data flow into a model object and updates the view whenever data changes.

How does the MVC controller work? ›

The controller doesn't have to worry about handling data logic, it just tells the model what to do. It processes all the business logic and incoming requests, manipulates data using the Model component, and interact with the View to render the final output.

What is the model-view-controller pattern MVC architecture and frameworks explained? ›

The MVC design pattern is a software architecture pattern that separates an application into three main components: Model, View, and Controller, making it easier to manage and maintain the codebase. It also allows for the reusability of components and promotes a more modular approach to software development.

What is MVC in simple words? ›

MVC (Model-View-Controller) is a pattern in software design commonly used to implement user interfaces, data, and controlling logic. It emphasizes a separation between the software's business logic and display.

What is the role of the controller in the model-view-controller MVC architecture? ›

The Controller in MVC acts as an intermediary between the View and the Model. It receives user input from the View, processes it (often with the help of Model data), and determines the appropriate response or action. The Controller updates the Model when data changes are necessary based on user interactions.

What is the working principle of MVC? ›

The MVC methodology separates an application's logic into three distinct layers:
  1. Model. The model layer is responsible for the application's data logic and storing and retrieving data from back-end data stores. ...
  2. View. The view layer provides the UI necessary to interact with the application. ...
  3. Controller.

What is the architecture of the MVC? ›

MVC is a pattern used to design user interfaces, data, and application logic to achieve separation of concerns. MVC separates applications into three groups of components: Model, View, and Controller. It helps applications to reduce their complexity and makes them more accessible in coding, debugging, and testing.

What is MVC for dummies? ›

MVC (Model-View-Controller) is a renowned architectural pattern forming countless software applications' backbone. This pattern carefully divides an application into three distinct yet interrelated components: the Model, the View, and the Controller. It separates the logic of the application from the display.

What are the three models of MVC? ›

In fact, in ASP.NET MVC, there are three distinct types of model: the domain model, view model and input model.

What is the difference between model-view-controller and MVC? ›

In MVC, the controller handles user input and updates the view accordingly, while in MVVM, the ViewModel acts as a mediator between the view and the model, managing data binding and providing a clean separation of concerns.

What is the main purpose of MVC? ›

MVC is primarily used to separate an application into three main components: Model, View, and Controller. This level is considered the lowest level when compared with the View and Controller. It primarily represents the data to the user and defines the storage of all the application's data objects.

What are the main concepts of MVC? ›

-MVC is an architectural pattern consisting of three parts: Model, View, Controller. Model: Handles data logic. View: It displays the information from the model to the user. Controller: It controls the data flow into a model object and updates the view whenever data changes.

What are some examples of MVC? ›

Examples of programming languages that use MVC are C, C++, C#, Java, Ruby, Smalltalk, and many more. The frameworks that use MVC are Angular, Express, Django, Flask, Laravel, Ruby on rails, and others.

How is MVC working? ›

The MVC framework separates an application into three main components: models, views, and controllers. Models are used to store data and business logic; views display data from a model on the screen; and controllers manage user interactions with these two components.

What does an MVC controller do? ›

A controller is responsible for controlling the way that a user interacts with an MVC application. A controller contains the flow control logic for an ASP.NET MVC application. A controller determines what response to send back to a user when a user makes a browser request.

What is the work flow of MVC? ›

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.

How does Spring MVC controller work? ›

Spring MVC Framework follows the Model-View-Controller architectural design pattern which works around the Front Controller i.e. the Dispatcher Servlet. The Dispatcher Servlet handles and dispatches all the incoming HTTP requests to the appropriate controller.

How does the controller know which view MVC? ›

If you don't pass in a specific view name to look for, it'll look for a view that matches the name of your controller action. So in this case, it will look for Index. cshtml. One variation of the View() method takes a single object (the model) as a parameter.

How does MVC flow work? ›

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.

What is the controller action method in MVC? ›

The Action Methods in ASP.NET MVC are responsible for executing requests and responding to them. The response is generated by default as an ActionResult. User interactions and actions correspond to one-to-one in most cases. The Controller class contains all public methods referred to as Action methods.

References

Top Articles
Latest Posts
Article information

Author: Prof. Nancy Dach

Last Updated:

Views: 5927

Rating: 4.7 / 5 (57 voted)

Reviews: 80% of readers found this page helpful

Author information

Name: Prof. Nancy Dach

Birthday: 1993-08-23

Address: 569 Waelchi Ports, South Blainebury, LA 11589

Phone: +9958996486049

Job: Sales Manager

Hobby: Web surfing, Scuba diving, Mountaineering, Writing, Sailing, Dance, Blacksmithing

Introduction: My name is Prof. Nancy Dach, I am a lively, joyous, courageous, lovely, tender, charming, open person who loves writing and wants to share my knowledge and understanding with you.