Learn ASP.NET MVC Step By Step For Beginners- Part Two

Introduction

This session is the second part of the series, where you will learn about ASP.NET MVC step by step for beginners. I suggest reading Part One of the blog.

Now, it’s time to get our hands dirty by creating our first ASP.NET MVC Application. We will create a new MVC 4 Application with Visual Studio 2012 for the Web and understand the basic building blocks of a MVC Application.

Create our first simple MVC Application

Step 1

First of all, open Visual Studio 2012.
  1. File-> New-> Project.

    Visual Studio 2012.

  2. Visual C#-> Web-> ASP.NET MVC 4 Web Application-> Name your project -> OK.

    Visual Studio 2012.

  3. From the New ASP.NET Project dialog, select MVC, as shown below. Also, select View Engine: Razor.

    Visual Studio 2012.

  4. Visual Studio creates the folder structure given below for MVC Applications, by default.

    Visual Studio 2012.

Step 2

Adding New Controller
  1. In Visual Studio, right click on the Controller folder -> select Add -> click Controller.

    Visual Studio 2012.

  2. It will open Add Controller dialog, as shown below.

    Visual Studio 2012.

  3. Write Controller name. Do not delete the word Controller. Remember, controller name must end with Controller.

    Visual Studio 2012.

  4. This will create HomeController class with Index method in HomeController.cs file under Controllers folder, as shown below.

    Visual Studio 2012.

    Controller

    1. Handle Requests to the application by the user.
    2. Get the data from the database.
    3. Specify a view that returns a response to the client.

Step 3

Add View
  1. Open a HomeController class -> right click inside Index method -> click Add View.

    Visual Studio 2012.

  2. In Add View dialogue box, keep the view name Index. It's good practice to keep the view name the same as the action method name, so that you don't have to specify view name explicitly in the action method, while returning the view.
  3. Select View Engine: Razor.

    Visual Studio 2012.

  4. The code is given below.

    Visual Studio 2012.

    View

    View is a user interface. View displays data from the model to the user and also enables them to modify the data.

Step 4 Adding Model

  1. Right click on Model folder -> Add -> click class.

    Visual Studio 2012.

  2. In the Add New Item dialog box, enter the class name 'MyFirstModel' and click Add.

    Visual Studio 2012.

  3. This will add new MyFirstModel class in model folder.

    Visual Studio 2012.

    Model

    1. Represents the Data of the Application.
    2. It maintains the data of the Application.
    3. Model objects retrieves and stores model state in a database.

    In this way, you can create your first MVC 4 Application, using Visual Studio 2012.

ASP.NET MVC Folder Structure

Visual Studio creates the folder structure given below for MVC Application, by default.

Visual Studio 2012.

Let's see the significance of each folder

  1. App_Data
    App_Data folder can contain application data files like LocalDB, .MDF files, XML files and other data related files.

  2. App_Start
    App_Start folder can contain the class files, which will be executed when the application starts. Typically, these would be config files like FilterConfig.cs, RouteConfig.cs etc.

    Visual Studio 2012.

Global.asax

Global.asax allows you to write the code that runs in response to Application level events, such as Application_Begin Request, application_start, application_error, session_start, session_end etc.

Packages.config

Packages.config file is managed by NuGet to keep the track of what packages and versions you have installed in the Application.

Web.config

Web.config file contains the Application level configurations.

Creating a simple hello world ASP.NET MVC Application.

See this example here

So far, we discussed MVC architecture in ASP.NET, including the flow of the user's request in ASP.NET MVC, Routing Engine in ASP.NET MVC, creating a first simple MVC Application, ASP.NET MVC folder structure, and creating a simple hello world ASP.NET MVC application.

We are going to discuss about data validation in MVC in the next part.