Professional Documents
Culture Documents
Learn MVC (Model View Controller) step by step in 7 days Day 1 - CodeProject
Contents
So, whats the agenda?
Other parts of MVC step by step in 7 days
So why MVC when ASP.NET code-behind is so good?
Problem number 1: Unit testing
Problem 2: The reality of separation of code and UI
Our HERO MVC (Model, view and controller)
Pre-requisite for MVC
Lab 1: Creating a simple Hello World ASP.NET MVC application
Video demonstration for Lab 1
Step 1: Create project
Step 2: Add controller
Step 3: Add view
Step 4: Run the application
So whats in the next lab?
Lab 2: Passing data between controllers and views
Video demonstration for Lab 2
Step 1: Create project and set view data
Step 2: Display view data in the view
So whats in the next lab?
Lab 3: Creating a simple model using MVC
Video demonstration for Lab 3
Step 1: Create a simple class file
Step 2: Define the controller with action
www.codeproject.com/Articles/207797/Learn-MVC-Model-View-Controller-step-by-step-in-7?display=Print
1/24
28/06/2013
Learn MVC (Model View Controller) step by step in 7 days Day 1 - CodeProject
This article series is divided into 7 days with 35 hands-on labs and every day we will do 5 labs which will help
us achieve our goals.
So get ready for day 1. For day 1, shown below is our agenda. We will start with an introduction, do a simple
Hello World, and finally in the 6 th lab, we will create a simple customer data entry screen using HTML Helper
classes.
Lab
No.
Lab description
YouTube video
demonstration
NA.
http://youtu.be/KAKxm4eQP24?
hd=1
2/24
28/06/2013
Learn MVC (Model View Controller) step by step in 7 days Day 1 - CodeProject
http://youtu.be/Fu9v2MIDlTA?
hd=1
http://youtu.be/0-UdqWy9lVc?
hd=1
Pending
Here you can watch my .NET interview questions and answers videos on various topics like WCF, Silverlight,
LINQ, WPF, Design Patterns, Entity Framework, etc.
www.codeproject.com/Articles/207797/Learn-MVC-Model-View-Controller-step-by-step-in-7?display=Print
3/24
28/06/2013
Learn MVC (Model View Controller) step by step in 7 days Day 1 - CodeProject
4/24
28/06/2013
Learn MVC (Model View Controller) step by step in 7 days Day 1 - CodeProject
FYI: Many developers talk about mock tests, rhino mocks, etc., but still it is cryptic and the complication
increases with session variables, view data objects, and ASP.NET UI controls creating further confusion.
5/24
28/06/2013
Learn MVC (Model View Controller) step by step in 7 days Day 1 - CodeProject
Thats where MVC comes to rescue. The code-behind is moved to a simple .NET class called Controller. Any
user request first comes to the Controller class, the Controller class then invokes the model, and attaches the
model to the view for display to the end user.
As this controller class is a simple .NET class we can reuse and also do unit testing easily. So lets see how we
can create MVC application using MVC template provided by visual studio.
6/24
28/06/2013
Learn MVC (Model View Controller) step by step in 7 days Day 1 - CodeProject
In case you want to spend more time with your family rather than reading the complete article you can watch
the following 5 minute YouTube video: http://youtu.be/KAKxm4eQP24?hd=1.
Once you click OK, you have a readymade structure with the appropriate folders where you can add
controllers, models, and views.
www.codeproject.com/Articles/207797/Learn-MVC-Model-View-Controller-step-by-step-in-7?display=Print
7/24
28/06/2013
Learn MVC (Model View Controller) step by step in 7 days Day 1 - CodeProject
Once you add the new controller, you should see some kind of code snippet as shown below:
public class Default1Controller : Controller
{
//
// GET: /Default1/
public ActionResult Index()
{
return View();
}
}
The add view pops up a modal box to enter the view name which will be invoked when this controller is called
as shown in the figure below. For now, keep the view name same as the controller name and also uncheck the
master page check box.
www.codeproject.com/Articles/207797/Learn-MVC-Model-View-Controller-step-by-step-in-7?display=Print
8/24
28/06/2013
Learn MVC (Model View Controller) step by step in 7 days Day 1 - CodeProject
Once you click on the OK button of the view, you should see a simple ASPX page with the below HTML code
snippet. In the below code snippet I have added This is my first MVC application.
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Index</title>
</head>
<body>
<div>
This is my first MVC application
</div>
</body>
</html>
www.codeproject.com/Articles/207797/Learn-MVC-Model-View-Controller-step-by-step-in-7?display=Print
9/24
28/06/2013
Learn MVC (Model View Controller) step by step in 7 days Day 1 - CodeProject
If you append the proper controller on the URL you should be able to see the proper view.
10/24
28/06/2013
Learn MVC (Model View Controller) step by step in 7 days Day 1 - CodeProject
session scope and view state has page scope. For MVC we would like to see the scope limited to the controller
and the view. In other words we would like to maintain data when the hit comes to the controller and reaches
the view and after that the scope of the data should expire.
Thats where the new session management technique has been introduced in the ASP.NET MVC framework,
i.e., ViewData.
www.codeproject.com/Articles/207797/Learn-MVC-Model-View-Controller-step-by-step-in-7?display=Print
11/24
28/06/2013
Learn MVC (Model View Controller) step by step in 7 days Day 1 - CodeProject
Create the class with three properties as shown in the below code snippet.
www.codeproject.com/Articles/207797/Learn-MVC-Model-View-Controller-step-by-step-in-7?display=Print
12/24
28/06/2013
Learn MVC (Model View Controller) step by step in 7 days Day 1 - CodeProject
13/24
28/06/2013
Learn MVC (Model View Controller) step by step in 7 days Day 1 - CodeProject
Note :- In case you are not able to see the model in the drop down , please compile your project
once.
The advantage of creating a strong typed view is you can now get the properties of the class in the view by
typing the model and . as shown in the below figure.
www.codeproject.com/Articles/207797/Learn-MVC-Model-View-Controller-step-by-step-in-7?display=Print
14/24
28/06/2013
Learn MVC (Model View Controller) step by step in 7 days Day 1 - CodeProject
Below is the view code which displays the customer property value. We have also put an i
fcondition which
displays the customer as a privileged customer if above 100 and a normal customer if below 100.
<body>
<div>
The customer id is <%= Model.Id %> <br />
The customer Code is <%= Model.CustomerCode %> <br />
<% if (Model.Amount > 100) {%>
This is a priveleged customer
<% } else{ %>
This is a normal customer
<%} %>
</div>
</body>
www.codeproject.com/Articles/207797/Learn-MVC-Model-View-Controller-step-by-step-in-7?display=Print
15/24
28/06/2013
Learn MVC (Model View Controller) step by step in 7 days Day 1 - CodeProject
As soon as the end user enters details and submits data it redirects to a screen as shown below. If the entered
amount is less than 100 it displays normal customer, else it displays privileged customer.
16/24
28/06/2013
Learn MVC (Model View Controller) step by step in 7 days Day 1 - CodeProject
The first step is to create the data entry page using the simple HTML form action tag as shown in the below
code snippet. The most important point to note in the below code snippet is that the action is pointing to the
controller action, i.e., D
isplayCustomer.
<form action="DisplayCustomer" method="post">
Enter customer id :- <input type="text" name="Id" /> <br />
Enter customer code :- <input type="text" name="CustomerCode" /><br />
Enter customer Amount :-<input type="text" name="Amount" /><br />
<input type="submit" value="Submit customer data" />
</form>
www.codeproject.com/Articles/207797/Learn-MVC-Model-View-Controller-step-by-step-in-7?display=Print
17/24
28/06/2013
Learn MVC (Model View Controller) step by step in 7 days Day 1 - CodeProject
The advantage of creating a strong typed view is you can now get the properties of the class in the view by
typing the model and . as shown in the below figure.
Below is the view code which displays the customer property value. We have also put an i
fcondition which
displays the customer as a privileged customer if above 100 and normal customer if below 100.
<body>
<div>
The customer id is <%= Model.Id %> <br />
The customer Code is <%= Model.CustomerCode %> <br />
www.codeproject.com/Articles/207797/Learn-MVC-Model-View-Controller-step-by-step-in-7?display=Print
18/24
28/06/2013
Learn MVC (Model View Controller) step by step in 7 days Day 1 - CodeProject
You should also be able to test above 100 and below 100 scenarios.
19/24
28/06/2013
Learn MVC (Model View Controller) step by step in 7 days Day 1 - CodeProject
Added to that, a lot of manual code was also written in the controller to flourish the object and send
data to the MVC view.
public class CustomerController : Controller
{
..
.
[HttpPost]
public ViewResult DisplayCustomer()
{
Customer objCustomer = new Customer();
objCustomer.Id = Convert.ToInt16(Request.Form["Id"].ToString());
objCustomer.CustomerCode = Request.Form["Id"].ToString();
objCustomer.Amount = Convert.ToDouble(Request.Form["Amount"].ToString()); ;
return View("DisplayCustomer", objCustomer);
}
}
In this lab we will see how to use MVC HTMLHelper classes to minimize manual coding and increase
productivity.
www.codeproject.com/Articles/207797/Learn-MVC-Model-View-Controller-step-by-step-in-7?display=Print
20/24
28/06/2013
Learn MVC (Model View Controller) step by step in 7 days Day 1 - CodeProject
By using the HTMLHelper class you can create any HTML control like T
extBox, Label, ListBox, etc., just by
invoking the appropriate function.
In order to create the f
ormtag for HTML we need to use Html.BeginForm. Shown below is the code
snippet for that:
<% using (Html.BeginForm("DisplayCustomer","Customer",FormMethod.Post))
{%>
-- HTML input fields will go here
<%} %>
The above code snippet will generate the below HTML code:
Enter customer id :- <input type="text" name="Id" /> <br />
To create a data entry screen like the one shown below, we need to the use the below code snippet.
www.codeproject.com/Articles/207797/Learn-MVC-Model-View-Controller-step-by-step-in-7?display=Print
21/24
28/06/2013
Learn MVC (Model View Controller) step by step in 7 days Day 1 - CodeProject
www.codeproject.com/Articles/207797/Learn-MVC-Model-View-Controller-step-by-step-in-7?display=Print
22/24
28/06/2013
Learn MVC (Model View Controller) step by step in 7 days Day 1 - CodeProject
So have a toast of beer for completing your first day of MVC labs.
License
This article, along with any associated source code and files, is licensed under The Code Project Open License
(CPOL)
www.codeproject.com/Articles/207797/Learn-MVC-Model-View-Controller-step-by-step-in-7?display=Print
23/24
28/06/2013
Learn MVC (Model View Controller) step by step in 7 days Day 1 - CodeProject
www.codeproject.com/Articles/207797/Learn-MVC-Model-View-Controller-step-by-step-in-7?display=Print
24/24