Tuesday, 23 June 2015

MVC for beginners

ASP.NET MVC Overview:

The Model-View-Controller (MVC) architectural pattern separates an application into three main components: the model, the view, and the controller.

Model

- Responsible for storing and retrieving data
- Maintenance of states.
- Notification of observers of change in state.
- Classes or references including .cs and .dll files

View

- Responsible for rendering UI model.
- User interaction (Text Box, List Box, Button, Label)
- Normally hold .cshtml, .aspx pages to show output.

Controller

- Responsible for responding to user input.
- Instructing model to process the user input.
- Make a bridge between view and model.


Every controller holds one or more than one action method(s). These action methods are calling during the execution through URL like let’s say the URL is localhost/Home/Index.

Here Home is the controller name and Index is the ActionMethod name. Now every Action method has its own View by its name. So here a view will be stored within the View à Home folder namedIndex.cshtml.

Whenever you will request for Home/Index the index.cshtml file will be shown within that View àHome folder


Now let’s see how to start this. Open your Visual Studio 2013 Choose project and set your name like as shown below



Now open your solution explorer in that we have three folders named Model, Controller and View and it will contain two web.config in solution one in the root folder and another one in the View folder.

Normally Home is the default Controller and Index is the default action method. If you open theRouteConfig.cs file in the App_Start folder you will come to see in the method RegisterRoutes that is default controller and ActionMethod is Home and Index respectively. If you want to change it you can do easily.

OK now let’s start with a simple program. Right click on the Controller folder and choose Create new Controller. Name it HomeController.





Now run your project and run with localhost/Home/Index and see whats the output. It will show the data of Index.cshtml within Home folder.

No comments:

Post a Comment