The Model-View-Controller (MVC) architectural pattern separates an application into three main components: the model, the view, and the controller. The ASP.NET MVC framework provides an alternative to the ASP.NET Web Forms pattern for creating Web applications. The ASP.NET MVC framework is a lightweight, highly testable presentation framework that (as with Web Forms-based applications) is integrated with existing ASP.NET features, such as master pages and membership-based authentication. The MVC framework is defined in the System.Web.Mvc assembly.
The MVC framework includes the following components:
Version | .NET Version | Release Date |
---|---|---|
MVC 1.0 | .NET 3.5 | 2009-03-13 |
MVC 2.0 | .NET 3.5/4.0 | 2010-03-10 |
MVC 3.0 | .NET 4.0 | 2011-01-13 |
MVC 4.0 | .NET 4.0/4.5 | 2012-08-15 |
MVC 5.0 | .NET 4.5 | 2013-10-17 |
MVC 5.1 | .NET 4.5 | 2014-01-17 |
MVC 5.2 | .NET 4.5 | 2014-08-28 |
MVC 6.0 | .NET 4.5 | 2015-11-18 |
Core MVC 1.0 | .NET 4.5 | 2016-07-12 |
Core MVC 1.1 | .NET 4.5 | 2016-11-18 |
ASP.NET MVC is open source web application framework. MVC itself is a design pattern which is built around three main components: model-view-controller.
Model - Models reflect your business objects, and are a means to pass data between Controllers and Views.
View - Views are the pages that render and display the model data to the user. ASP.NET MVC Views are typically written using Razor syntax.
Controller - Controllers handle incoming HTTP requests from a client, and usually return one or more Models to an appropriate View.
The ASP.NET MVC features:
Simple MVC application
We are going to create simple MVC application which displays person details. Create new MVC project using Visual Studio. Add new model named Person to Models folder as following:
public class Person
{
public string Surname { get; set; }
public string FirstName { get; set; }
public string Patronymic { get; set; }
public DateTime BirthDate { get; set; }
}
Add new controller to Controllers folder:
public class HomeController : Controller
{
//Action Method
public ActionResult Index()
{
// Initialize model
Person person = new Person
{
Surname = "Person_SURNAME",
FirstName = "Person_FIRSTNAME",
Patronymic = "Person_PATRONYMIC",
BirthDate = new DateTime(1990, 1, 1)
};
// Send model to View for displaying to user
return View(person);
}
}
Finally add View to /Views/Home/ folder named Index.cshtml:
@* Model for this view is Person *@
@model Hello_MVC.Models.Person
<h2>Hello @Model.FirstName !</h2>
<div>
<h5>Details:</h5>
<div>
@Html.LabelFor(m => m.Surname)
@Html.DisplayFor(m => m.Surname)
</div>
<div>
@Html.LabelFor(m => m.FirstName)
@Html.DisplayFor(m => m.FirstName)
</div>
<div>
@Html.LabelFor(m => m.Patronymic)
@Html.DisplayFor(m => m.Patronymic)
</div>
<div>
@Html.LabelFor(m => m.BirthDate)
@Html.DisplayFor(m => m.BirthDate)
</div>
</div>