Overview

In the last couple of weeks I was just randomly going through a questionnaire section and found out there are many questions on MVC, particularly at the beginner level, or newbie level to ASP.NET you could say. There are many question about which is best, webforms or MVC, and if MVC,  which version. As there are some nice articles which are related to MVC on C Sharp Corner, this article will brief you about MVC, versions, a sample  application, and answer the question:  Why MVC?

Introduction

Today if you do market research the demand for MVC is huge and it's growing in numbers. Each and every company these days needs a MVC developer because whenever a new project for development comes they just want to develop their product in MVC. MVC Version 5 has been released with new features and the popularity of MVC is growing day by day. So let’s start.

If your existing project is in webforms and you find it perfectly normal, you will say why MVC, why not webforms? This URL here will give you a perfect picture as to why.

MVC (Model-View-Controller)

First let's see a simple diagram to give you an idea of how MVC Works.



As you can see in the above simple representation of how MVC works you see IIS, as MVC is a framework that is used to build web applications.

Model

Model is the part of your application that handles all logic sections; such as a request from user to insert or retrieve data from database.

View

Views are created from Model Data and the main role of View is display data to Users.

Controller

This handles all validations from users, reads data from view and also sends data to Model.

  • So let's start with an ASP.NET MVC application;  before that download the setup of MVC. To download the setup of MVC go to this link and just download --  it’s a very simple wizard, just click next and MVC setup will be installed . Refe to this URL for download.

  • After installation just go your control panel and make sure it's successfully installed.



    Microsoft Asp.NET MVC 4 is successfully installed. Now, its time to create an application.

    Open Visual Studio ->File->New Project.



    Select ASP.NET MVC 4 Web Application ->Give Suitable name as MVCTestApplication.



    Now select Empty Application and Click Ok.

  • Now you will see this:



    Controllers, Models, and Views got created in our application. By default our controller does not have any file so we add a new file in a controller.

  • Right Click on Controller->Add->Controller.



  • Name the controller as HomeController and Click ok.



  • Now you will see this when you Click Ok:



    Now here HomeController is our Class and Action is Index. If you write any code in actionable index, it will be rendered on the page . Now we will see HomeController is called by default.



  • In App_Start when you see Route.Config File you will see the following code.


    We will see a version of our MVC, and see which version it's using as we had selected MVC 4.

  • HomeController.cs



    We will see which version of MVC  -- just run the solution:



  • You can also Check in the references folder system.web.mvc



  • Now we will Create a Hello World example



  • Just run the application and you will see the following output.



    Now just type the following URL: http://localhost:49414/Home/Index.



  • You will see the same output is getting displayed:



  • Now we will write another action method and see what is getting displayed.



  • As you can see I had written another Action Method HelloAll() now we will run the application and see what is getting displayed.



  • You will see Hello Csharpcorner got displayed -- why? Now go to RouteConfig.cs file and see that our Home Controller and Index action method is called by default . Now we will change the Action Method in URL and see what happens.



  • This File says that default document get loaded as our default document here is HomeController and action is Index.

  • Just Type this URL.



    You will see the output as,



    Our HellAll() Action Method now has been invoked and How Are You? has been displayed . You can also change RouteConfig.cs File.

  • Let's rename our Index() Action Method and see what output it generates



    I renamed it to IndexTest() . Now run application .



    Now instead of displaying Hello Csharpcorner we got an error: resource not found. Why?  Because we renamed our Action method and we didn’t configure in our Routecinfig file.

  • Rename the Action method as Index().

Conclusion

This is all about a basic MVC application. In the next article I will explain more about controllers, models and views. Hope this was helpful to those who are new to MVC.