Introduction to ASP.NET MVC

In this article, we will discuss ASP.NET MVC. MVC stands for Model-View-Controller and it is a framework for developing web applications which have become so popular these days. Earlier developers would go for ASP.NET to create a web application, MVC is an extension to it.

Why MVC?

The basic need of introducing MVC was to make complex application development easy as it takes time to develop a complex application in ASP.NET. MVC is a lightweight framework as compared to traditional ASP.NET Web Forms. The purpose of MVC is to separate the content from the presentation and data processing from the content. One main thing is that MVC has separate the view from the code i.e., unlike Web Forms where “*.aspx” is attached to “*.aspx.cs” here in MVC View is a separate entity entirely.

ASP.NET MVC moves around three basic concepts:

  • Model
  • View
  • Controller

Model: Model is a part of the application which implements the logic for the application. It is used to retrieve and store data in a database. It is also used for business logic separation in the application. It contains the classes.

Example:

public class Order
    {
        public string OrderId { get; set; }
        public int CustomerId { get; set; }
    }

Here { get; set; } is the same like getter and setter in android if you are familiar with. It will get the value from the controller and set it in a particular variable.

View: View is a component that forms the application’s UI. In MVC we use Razor Syntax. The extension of the view has “*.cshtml” instead of “.aspx” which was used in ASP.NET.

MVC view can contain basic HTML, javascript and C# code also.

@{
    string title = "Index";
}

The C# code starts with @ sign in the view and we must have to specify the curly braces {  }

Controller: Controller is an intermediate between the View and Model. All the user interaction is done through Controller only. It controls the user interaction with the application. All the controller names end with Controller like HomeController

Example:

public ActionResult Home()
{
     return View();
}