Getting Started with Adobe After Effects - Part 6: Motion Blur


Upload Image Close it
Select File

This tutorial will help you to get started with ASP.NET MVC

Getting started with ASP.NET MVC

Getting Started with ASP.NET MVC - Part 2: Your first application with ASP.NET MVC - How it works.

Apr 24 2012 12:00AM by Jalpesh   

In the first chapter, we have learned what ASP.NET MVC is and how to install ASP.NET MVC. In this chapter, we will create a basic ASP.NET MVC application and will learn how its works.

Get Started- Creating first application

As we have already installed ASP.NET MVC, we are ready to create our first ASP.NET Application. So, to create a new ASP.NET MVC 3 application goto File-> New Project. It will open up a dialog for new project like following.

New Project

Here, select ASP.NET Web application and click OK. It will give a new dialogbox as in the image below. In this dialog box, we have options to create internet/intranet/empty application. Here empty application will create an empty application. Internet application will create an application with AccountController with form authentication. Intranet application will create application, which use windows authentication. In addition, it will have an option to choose view engine. In this tutorial series, we are going to learn Razor syntax. Therefore, I am going to create application with Razor View Engine. You can also create unit test project with this. However, for time being we are not adding it. It also supports HTML5 markup but to understand it better, we are going with simple HTML markup.

New ASP.NET MVC Application

Here our purpose is to learn ASP.NET MVC with our first ASP.NET MVC application. Therefore, as we are creating internet application, I have selected Internet Application in above dialogbox. Once you click OK, it will create ASP.NET MVC application just like following.

Solution Explorer

As you can see in above solution explorer screenshot, it has created three folders – Models Views and Controllers, which contains different controllers, views and models that are needed in this application. As we are using defualt Microsoft template it has created several views, controller and models

How to run application?

As of now, we have created our application. It is now time to run our application. You can run application in three ways. If you want to debug your application, then you need to run it via pressing ‘F5’. If you don’t want to debug it, then you can press ‘Ctrl+F5’ to run application. You can also run that application via clicking on Green Arrow on Visual Studio as shown in the screen below:

Green Arrow

Now, once you press the green arrow, it will run ASP.NET MVC application as below:

Application in browser

How its works?

As we have created a basic application, now let us understand how it works. There are three main parts of the application – Controller, Models and Views. You can also say, these are three different layers of the application. As in solution explorer, you have seen that there are three diffrent folders are created for that.

Now let’s see the code for controllers. Following is code for the Home Controllers.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MyFirstMVC.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            ViewBag.Message = "Welcome to ASP.NET MVC!";

            return View();
        }

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

As you can see, there are different Action Results written in controllers. This action results will return a view based on that ASP.NET MVC application output a view on browser. The action results are mapped to a particular view. For example, Index action result is mapped to a view name Index.shtml. Following is a code for the Index view.

@{
    ViewBag.Title = "Home Page";
}

<h2>@ViewBag.Message</h2>
<p>
    To learn more about ASP.NET MVC visit <a href="http://asp.net/mvc" title="ASP.NET MVC Website">http://asp.net/mvc</a>.
</p>

As you can see in controller, we are setting up viewbag message and in view you can see its printing that message. In controller, we have written some message and once you run that application, it will show that message in browser as shown in the screenshot above.

Models folder contains classes that represent particular entities like users, products, accounts etc. It contains all properties and different data attributes required for system, such as, attributes which will tell the system that this field is required for the system. Example of the Model classes is as follows:

public class ChangePasswordModel
    {
        [Required]
        [DataType(DataType.Password)]
        [Display(Name = "Current password")]
        public string OldPassword { get; set; }

        [Required]
        [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
        [DataType(DataType.Password)]
        [Display(Name = "New password")]
        public string NewPassword { get; set; }

        [DataType(DataType.Password)]
        [Display(Name = "Confirm new password")]
        [Compare("NewPassword", ErrorMessage = "The new password and confirmation password do not match.")]
        public string ConfirmPassword { get; set; }
    }

Here, you can see all the attributes. In addition, we can create view, which will bind to this particular class. We will explore this in next chapter in details.

Therefore, that is it. In next chapter, we will understand View, Model and Controller with one real example from scratch to give a better idea.


Jalpesh
15 · 11% · 3478
9



Submit

3  Comments  

  • Good start Jalpesh

    commented on Apr 24 2012 5:35AM
    Kirti M.Darji
    10 · 16% · 4983
  • Thanks kirti

    commented on Apr 25 2012 2:02AM
    Jalpesh
    15 · 11% · 3478
  • BR is going to be the best step by step series of learning. This is one example. Thanks @Jalpesh

    commented on Apr 28 2012 10:59AM
    Manas Ranjan Dash
    39 · 5% · 1482

Your Comment


Sign Up or Login to post a comment.

"Getting Started with ASP.NET MVC - Part 2: Your first application with ASP.NET MVC - How it works." rated 5 out of 5 by 9 readers
Getting Started with ASP.NET MVC - Part 2: Your first application with ASP.NET MVC - How it works. , 5.0 out of 5 based on 9 ratings
    Copyright © Rivera Informatic Private Ltd Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising      [ZULU1097]