ASP.NET MVC

Introduction

MVC is an architectural design pattern which is used to develop the user interfaces which are maintainable, reusable, and testable. It is used for separation of concerns.
MVC stands for Model, View, and Controller. MVC separates the application into three component like Model, View, and Controller. We will learn in detail what Model, View, and Controller is.

What is the design pattern?

A design pattern is a solution to the problems that occur in software development. Design pattern helps us in applications development to resolve problems easily.
Example of design pattern

What is ASP.NET MVC

ASP.NET MVC is a web framework from Microsoft which is used to develop the web application. ASP.NET MVC use MVC architecture pattern. ASP.NET MVC are introduced in 2009 by Microsoft.

Advantages of using ASP.NET MVC

Separation of code
Loosely coupling
Parallel Development
Easy to perform unit testing
TDD (Test Driven Development)

Model

Sample code of model as follows,
  1. namespace MVCDemo.Models
  2. {
  3. public class Employee
  4. {
  5. public int EmpId { get; set; }
  6. public string Name { get; set; }
  7. public string Email { get; set; }
  8. public double Salary { get; set; }
  9. }
  10. }

View

  • The view is responsible for UI (user interface).
  • View displays the data coming from the model.
  • The view is an HTML template which will be binding and displaying HTML controls with data.
  • The “.cshtml” file use the Razor view engine. And .cshtml views are use C# programming.
The view contains the following extension depends on languages.
  1. .aspx
  2. .asp
  3. .html
  4. .cshtml
  5. .vbhtml
Following are the types of view in ASP.NET MVC,
  1. Layout View
  2. Partial view
  3. Normal view

Controller

  • A controller can contain action and non-action method. It is used to handle the user request coming from the browser. It will check the request from the browser and identify the action method and return the respective view.
  • A controller is inherited from “ControllerBase” class which is inside the “System.Web.Mvc” namespace.
  • While creating controller remember it always has the suffix “Controller” keyword. Default Controller is “HomeController” and “Index” view.
Sample code of Controller as follows,
  1. namespace MVCDemo.Controllers
  2. {
  3. public class HomeController : Controller
  4. {
  5. // GET: Home
  6. public ActionResult Index()
  7. {
  8. return View();
  9. }
  10. }
  11. }
Following are diagrams of ASP.NET MVC,
ASP.NET MVC
ASP.NET MVC
As per the above figure, user request enters the URL on the browser, the given request goes to the server and calls the routing which will execute the appropriate controller. And based on the request the controller executes the appropriate controller action method. It will pass the request to model if the controller action method has data from the database. After completing this process controller returns the response to the user.
I hope you understand the introduction of ASP.NET MVC and the advantages of using ASP.NET MVC.