Author - StudySection Post Views - 449 views
mvc-framework

Introduction to MVC Framework With Example

MVC

MVC stands for Model View Controller and it is a part of three components namely Model, View, and Controller.

Controller:– It controls the flow of the application. It works as a mediator between View and Model. It will get a request from View and fetch data from the Model according to View request and respond back to the View.
View:– It is used to display data to the user that is present in the Model. To link with Model it will invoke the Controller.
Model:– It helps to communicate with the database. It sends data to a database and retrieves data from the database.

For Example,
When the user enters URL in the browser, the request will be accepted by the Controller and the Controller will handle the request and communicate with the Model to send data to View to display it to the user.

  • User interacts with the View
  • View interacts with the Controller
  • Controller updates Model about the request of View
  • Model will return according to Controller request
  • Controller will send a response back to View.

Features of MVC

  • Readability of the code becomes easy
  • Clean code makes everyone understand the flow
  • Clear separation of logic, models and HTML code
  • Code can be reused

Let’s see an example of Model View Controller from daily life:

When customers go to a restaurant, a waiter comes and takes orders. It is the duty of the waiter to serve your order but he will not cook the food. He will go to the kitchen and tell the cook what they need to prepare. When the food will be prepared by the cook, he/she will call the waiter and the waiter will serve the food to the customer.
So, the same scenario occurs when you create an application. You will need to create frontend code which will be written in View file. Any data that will be needed to show from the database will be passed by Controller to View by fetching data from the Model.

If there is anything common that is needed to be called very often, you can create a helper class for that.

Why do we need to use MVC?

  • MVC is the standard way to create a code. It is a good practice to follow the MVC pattern while coding.
  • Code can be reused as it is clean and it is easily understood by other programmers.
  • Testing can be performed easily.
  • Security and performance are good.

Example: We need to create a simple application according to the following points

  • Display list of links
  • Create a form where the user can submit the link
  • Validate the form
  • Insert into database
  1. To display you need to create a View file with form fields Link, description, and title with a submit button
  2. To validate you need to write code of validation in Controller
  3. To insert the link, description, and title, the code will be written in the Model.

Knowledge of .NET is quite rewarding in the IT industry. If you have got some skills in the .NET framework then a .NET certification from StudySection can prove to be a good attachment with your resume. You can go for a foundation level certificate as well as an advanced level certificate in the .NET framework.

Leave a Reply

Your email address will not be published.