asp.net-mvc Getting started with asp.net-mvc

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Remarks

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:

  • Models. Model objects are the parts of the application that implement the logic for the application's data domain. Often, model objects retrieve and store model state in a database. For example, a Product object might retrieve information from a database, operate on it, and then write updated information back to a Products table in a SQL Server database. In small applications, the model is often a conceptual separation instead of a physical one. For example, if the application only reads a dataset and sends it to the view, the application does not have a physical model layer and associated classes. In that case, the dataset takes on the role of a model object.
  • Views. Views are the components that display the application's user interface (UI). Typically, this UI is created from the model data. An example would be an edit view of a Products table that displays text boxes, drop-down lists, and check boxes based on the current state of a Product object.
  • Controllers. Controllers are the components that handle user interaction, work with the model, and ultimately select a view to render that displays UI. In an MVC application, the view only displays information; the controller handles and responds to user input and interaction. For example, the controller handles query-string values, and passes these values to the model, which in turn might use these values to query the database.

Versions

Version.NET VersionRelease Date
MVC 1.0.NET 3.52009-03-13
MVC 2.0.NET 3.5/4.02010-03-10
MVC 3.0.NET 4.02011-01-13
MVC 4.0.NET 4.0/4.52012-08-15
MVC 5.0.NET 4.52013-10-17
MVC 5.1.NET 4.52014-01-17
MVC 5.2.NET 4.52014-08-28
MVC 6.0.NET 4.52015-11-18
Core MVC 1.0.NET 4.52016-07-12
Core MVC 1.1.NET 4.52016-11-18

Hello MVC!

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:

  1. Ideal for developing complex but light weight applications
  2. It provides an extensible and pluggable framework which can be easily replaced and customized. For example, if you do not wish to use the in-built Razor or ASPX View Engine, then you can use any other third-party view engines or even customize the existing ones.
  3. Utilizes the component-based design of the application by logically dividing it into Model, View and Controller components. This enables the developers to manage the complexity of large-scale projects and work on individual components.
  4. The MVC structure enhances the test-driven development and testability of the application since all the components can be designed interface-based and tested using mock objects. Hence the ASP.NET MVC Framework is ideal for projects with large team of web developers.
  5. Supports all the existing vast ASP.NET functionalities such as Authorization and Authentication, Master Pages, Data Binding, User Controls, Memberships, ASP.NET Routing, etc.
  6. It does not use the concept of View State (which is present in ASP.NET). This helps in building applications which are light-weight and gives full control to the developers.

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>
 


Got any asp.net-mvc Question?