What is ASP MVC
Here we look at What is ASP MVC?
Just what is ASP.NET MVC? ASP.NET MVC(Model, View Controller) is the Microsoft implementation of the MVC pattern which was originally formed in the late 1970's as stated on that Wiki page. Microsoft introduced the ASP.NET version in 2009, called the ASP.NET MVC Framework. ASP.NET MVC is a methodology, a pattern in the way one produces a website using ASP.NET. There are certain rules, different ways of doing things to standard ASP.NET, which is useful for Agile development. It kind of standardises the way developers work, so when in a team, if all the developers are working using MVC, the structure of their code is more similar. So if you have developers who all like to code ASP.NET in completely different ways, ASP.NET MVC helps to organise this a little bit. It also enforces some strengths of ASP.NET, such as URL routing, using jQuery as standard and cleaner HTML for SEO purposes.
What is ASP.NET MVC - Model?
Consider the Model to be some data, or business logic. For instance a LINQ to SQL file.
What is ASP.NET MVC - View?
The View is either an .aspx page or .ascx user control, pretty much just UI (User Interface) elements.
What is ASP.NET MVC - Controller?
The controller controls things (sorry, sense of humour), that is, it controls the UI validation logic which is contained in the controller, it makes the connection between the Model and the View. It's basically a C# class file, where the class inherits from the System.Web.Mvc.Controller class. This class contains Actions.
What is ASP.NET MVC - Action?
The term Action isn't part of the MVC acronym, but it is an element in ASP.NET MVC. Actions are really methods which are part of the Controller class, and they are of type ActionResult, and they return Views as their return value.
What is ASP.NET MVC?
Visual Studio 2010 Express comes with ASP.MVC 2 ready to go with a built in project template. Amongst other inclusions, we have the following folders:
- Models - contains a .cs file, called AccountModels.cs.
- Controllers - contains .cs files, all filenames end with the word 'Controller', eg. AboutController.cs.
- Views - contains sub-folders, eg. Home, and each one contains .aspx files such as Index.aspx.
The provided project MVC template contains enough for a demo site to run, but I'm going to add to these items to get a feel for MVC.
Models - I added a LINQ to SQL file to the Models folder, this is my model. In this folder the DataContext's namespace has had .Models added to it's namespace (default Visual Studio behaviour for class files in sub-folders).
Controllers - I added a C# class file called PageController.cs, which means it's a controller called Page. (The MVC convention is that there should be a Controller for every entity in the model.) I added this file to the Controllers folder. To the file I added an Action, the code is hown here:
public ActionResult PageList()
Models.DataClasses1DataContext dcb = new Models.DataClasses1DataContext();
Views - I added a folder called Page, and then I added a View (using the Visual Studio wizard, right click the folder, Add - View). I ticked it to be strong typed based on my Page model (the LINQ to SQL class) and named it PageList. This caused a PageList.aspx page to be created under my sub-folder Page. As I selected 'View Content' to be 'List', the .aspx page contains the databinding code to display my LINQ to SQL datasource on screen. The MVC convention is that there should be a folder for every Controller. The pages are related to that specific Controller, thus aiding with the URL routing. There is also a 'Shared' folder under Views which is used for sharing Views with all the Controllers, general purpose Views (pages).
A link needs to be added so I can navigate to the page, so I used the HTML Helper code below:
<%: Html.ActionLink("Pages", "PageList", "Page") %>
This HTML Helper generates a URL routing friendly link to my page, without having to worry about the exact syntax. The first parameter is the text of the link, the second string is the name of the Action to use (method in Controller class), and the third string is the name of the Controller. The C# code above for the Action in the Controller, returns a View, where the View contains the datasource.
Build and deploy and the datasource is displayed on that page.