SlideShare a Scribd company logo
1 of 36
ASP.NET MVC Part I
Ventsislav Popov
Developer Evangelist at Microsoft
ventsypopov.com
ventsypopov.com
Agenda
 Beforehand – ASP.NET Web Forms
 What is MVC
 What is ASP.NET MVC?
 Models
 Views
 Controllers
 Validation
 Routing
 UnitTests
 View engines
2
ventsypopov.com
ASP.NET Web Forms
 Rich controls and tools
 Postbacks
 Event driven web development
 Viewstate
 Less control over the HTML
 Hard to test
 Rapid development
3
ventsypopov.com
Let’s chat for a bit…
4
ventsypopov.com
What is MVC
5
Model –View - Controller
6
 Controller - responsible for handling all user
input
 Model - represents the logic of the application
 View - the visual representation of the model
ventsypopov.com
ASP.NET MVC
 More control over HTML
 No Codebehind
 Separation of concerns
 Easy to test
 URL routing
 No postbacks
 NoViewState
7
ventsypopov.com
Models
 The model should contain all of the application
business logic, validation logic, and database
access logic.
 ASP.NET MVC is compatible with any data
access technology (for example LINQ to SQL)
 All .edmx files, .dbml files etc. are located in
the Models folder.
8
ventsypopov.com
Custom View Models
9
 When you combine properties to display on a
View
namespace ContosoUniversity.ViewModels
{
public class AssignedCourseData
{
public int CourseID { get; set; }
public string Title { get; set; }
public bool Assigned { get; set; }
}
}
ventsypopov.com
Creating a Model - DEMO
10
ventsypopov.com
What is Controller?
 It is a class
 Derives from the base
System.Web.Mvc.Controller class
 Generates the response to the browser request
11
public class HomeController : Controller
{
public ActionResult Index()
{
ViewBag.Message = "Welcome to ASP.NET MVC!";
return View();
}
public ActionResult About()
{
return View();
}
}
ventsypopov.com
Controller Actions
 Public method of the Controller class
 Cannot be overloaded
 Cannot be a static method
 Returns action result
12
public ActionResult About()
{
return View();
}
ventsypopov.com
Action Results
 Controller action response to a browser
request
 Inherits from the base ActionResult class
 Different results types
13
ventsypopov.com
Implement a Controller -
DEMO
14
ventsypopov.com
Action Results Types
 ViewResult
 EmptyResult
 RedirectResult
 JsonResult
 JavaScriptResult
 ContentResult
 FileContentResult
 FileStreamResult
 FilePathResult
15
ventsypopov.com
Controller base class
methods
 View
 Redirect
 RedirectToAction
 RedirectToRoute
 Json
 JavaScriptResult
 Content
 File
16
ventsypopov.com
Views
 Most of the Controller Actions return views
 The path to the view is inferred from the name
of the controller and the name of the
controller action.
 ViewsControllerNameControllerAction.aspx
 A view is a standard (X)HTML document that
can contain scripts.
 script delimiters <% and %> in the views
17
ventsypopov.com
Pass Data to a View
 WithViewData:
 ViewData["message"] = "Hello World!";
 Strongly typedViewData:
 ViewData.Model = OurModel;
 WithViewBag:
 ViewBag.Message = "Hello World!";
18
ventsypopov.com
Post data to a controller
 Verb Attributes
 The action method in the controller accepts the
values posted from the view.
 The view form fields must match the same
names in the controller.
19
ventsypopov.com
[HttpPost]
public ActionResult Edit(Movie movie)
{
if (ModelState.IsValid)
{
db.Entry(movie).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(movie);
}
Explore a View - DEMO
20
ventsypopov.com
HTML Helpers
 Methods which typically return string.
 Used to generate standard HTML elements
 textboxes, dropdown lists, links etc.
 Example: Html.TextBox() method
 Usage is optional
 You can create your own HTML Helpers
21
ventsypopov.com
Validation
 Two types of validation error messages
 generated before the HTML form fields are
bound to a class
 generated after the form fields are bound to the
class
 Model State
 Validation Helpers
 Html.ValidationMessage()
 Html.ValidationSummary()
22
ventsypopov.com
Implement validation-
DEMO
23
ventsypopov.com
Routing
 The Routing module is responsible for
mapping incoming browser requests to
particular MVC controller actions.
 Two places to setup:
 Web.config file
 Global.asax file
24
ventsypopov.com
Routing Setup
 Web.config file
25
<system.web>
<httpModules>
…
<system.web>
<httpHandlers> …
<system.webServer>
<modules> …
<system.webServer>
<handlers>
…
ventsypopov.com
Routing Setup
Global.asax file
26
public class MvcApplication : System.Web.HttpApplication
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home",
action = "Index", id = "" }
);
}
protected void Application_Start()
{
RegisterRoutes(RouteTable.Routes);
}
}
ventsypopov.com
URL Example
http://www.mysite.com/Home/About/6
{controller} = Home
{action} = About
{id} = 6
27
ventsypopov.com
ventsypopov.com
Routing example - DEMO
28
ventsypopov.com
Unit Tests
 Used for the business logic (not DAL orView
logic).
 Test individual “unit”of code
 Make the code safe to modify
 Mock Object framework
 When you lack “real” objects
 Create mocks for the classes in the application
 Test with mock objects
29
ventsypopov.com
Unit Tests - DEMO
30
ventsypopov.com
View Engines
 Handles the rendering of the view to UI
(html/xml);
 Different view engines have different syntax
 ASP.NET MVC 3 Pre-includedView Engines:
 Web Forms
 Razor
31
ventsypopov.com
View Engines - DEMO
32
ventsypopov.com
Things to remember
 What MVC stands for
 How ASP.NET MVC differs from Web Forms
 Where is routing configured
 How to validate business logic
 How to use helpers
 Unit tests basics
 Choice between “View Engines”
33
ventsypopov.com
Useful sites
 http://www.asp.net/mvc
 http://msdn.microsoft.com/en-
us/library/dd394709.aspx
 http://stackoverflow.com/
 http://jquery.com/
34
ventsypopov.com
Questions?
ASP.NET MVC
ventsypopov.com
Email: vepopov [at] microsoft.com
Twitter: @v_popov
Time to wake up :)
36

More Related Content

Similar to ASP.NET-MVC-Part-1.ppt

Asp.Net Mvc
Asp.Net MvcAsp.Net Mvc
Asp.Net Mvcmicham
 
ASP.NET MVC Fundamental
ASP.NET MVC FundamentalASP.NET MVC Fundamental
ASP.NET MVC Fundamentalldcphuc
 
Model view controller (mvc)
Model view controller (mvc)Model view controller (mvc)
Model view controller (mvc)M Ahsan Khan
 
MVC From Beginner to Advance in Indian Style by - Indiandotnet
MVC From Beginner to Advance in Indian Style by - IndiandotnetMVC From Beginner to Advance in Indian Style by - Indiandotnet
MVC From Beginner to Advance in Indian Style by - IndiandotnetIndiandotnet
 
Developing ASP.NET Applications Using the Model View Controller Pattern
Developing ASP.NET Applications Using the Model View Controller PatternDeveloping ASP.NET Applications Using the Model View Controller Pattern
Developing ASP.NET Applications Using the Model View Controller Patterngoodfriday
 
Presentation Thesis
Presentation ThesisPresentation Thesis
Presentation ThesisNaim Latifi
 
ASP.net MVC Introduction Wikilogia (nov 2014)
ASP.net MVC Introduction Wikilogia (nov 2014)ASP.net MVC Introduction Wikilogia (nov 2014)
ASP.net MVC Introduction Wikilogia (nov 2014)Hatem Hamad
 
03 asp.net session04
03 asp.net session0403 asp.net session04
03 asp.net session04Mani Chaubey
 
Introduction to ASP.NET MVC
Introduction to ASP.NET MVCIntroduction to ASP.NET MVC
Introduction to ASP.NET MVCSunpawet Somsin
 
Asp.Net MVC Framework Design Pattern
Asp.Net MVC Framework Design PatternAsp.Net MVC Framework Design Pattern
Asp.Net MVC Framework Design Patternmaddinapudi
 
Introduction to Struts 1.3
Introduction to Struts 1.3Introduction to Struts 1.3
Introduction to Struts 1.3Ilio Catallo
 

Similar to ASP.NET-MVC-Part-1.ppt (20)

Asp.Net MVC Intro
Asp.Net MVC IntroAsp.Net MVC Intro
Asp.Net MVC Intro
 
Asp.Net Mvc
Asp.Net MvcAsp.Net Mvc
Asp.Net Mvc
 
Asp.net mvc
Asp.net mvcAsp.net mvc
Asp.net mvc
 
ASP.MVC Training
ASP.MVC TrainingASP.MVC Training
ASP.MVC Training
 
MVC 4
MVC 4MVC 4
MVC 4
 
ASP.NET MVC Fundamental
ASP.NET MVC FundamentalASP.NET MVC Fundamental
ASP.NET MVC Fundamental
 
Model view controller (mvc)
Model view controller (mvc)Model view controller (mvc)
Model view controller (mvc)
 
MVC From Beginner to Advance in Indian Style by - Indiandotnet
MVC From Beginner to Advance in Indian Style by - IndiandotnetMVC From Beginner to Advance in Indian Style by - Indiandotnet
MVC From Beginner to Advance in Indian Style by - Indiandotnet
 
ASp.net Mvc 5
ASp.net Mvc 5ASp.net Mvc 5
ASp.net Mvc 5
 
Developing ASP.NET Applications Using the Model View Controller Pattern
Developing ASP.NET Applications Using the Model View Controller PatternDeveloping ASP.NET Applications Using the Model View Controller Pattern
Developing ASP.NET Applications Using the Model View Controller Pattern
 
Presentation Thesis
Presentation ThesisPresentation Thesis
Presentation Thesis
 
MVC 6 Introduction
MVC 6 IntroductionMVC 6 Introduction
MVC 6 Introduction
 
ASP.net MVC Introduction Wikilogia (nov 2014)
ASP.net MVC Introduction Wikilogia (nov 2014)ASP.net MVC Introduction Wikilogia (nov 2014)
ASP.net MVC Introduction Wikilogia (nov 2014)
 
MVC - Introduction
MVC - IntroductionMVC - Introduction
MVC - Introduction
 
03 asp.net session04
03 asp.net session0403 asp.net session04
03 asp.net session04
 
Asp.net mvc
Asp.net mvcAsp.net mvc
Asp.net mvc
 
Introduction to ASP.NET MVC
Introduction to ASP.NET MVCIntroduction to ASP.NET MVC
Introduction to ASP.NET MVC
 
Asp.Net MVC Framework Design Pattern
Asp.Net MVC Framework Design PatternAsp.Net MVC Framework Design Pattern
Asp.Net MVC Framework Design Pattern
 
MSDN - ASP.NET MVC
MSDN - ASP.NET MVCMSDN - ASP.NET MVC
MSDN - ASP.NET MVC
 
Introduction to Struts 1.3
Introduction to Struts 1.3Introduction to Struts 1.3
Introduction to Struts 1.3
 

Recently uploaded

办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样umasea
 
The Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfThe Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfPower Karaoke
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024StefanoLambiase
 
What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....kzayra69
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作qr0udbr0
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Andreas Granig
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...OnePlan Solutions
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureDinusha Kumarasiri
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...gurkirankumar98700
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio, Inc.
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptkotipi9215
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataBradBedford3
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Hr365.us smith
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmSujith Sukumaran
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesPhilip Schwarz
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideChristina Lin
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxTier1 app
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - InfographicHr365.us smith
 

Recently uploaded (20)

办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
 
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort ServiceHot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
 
The Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfThe Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdf
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
 
What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with Azure
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.ppt
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
 
Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)Recruitment Management Software Benefits (Infographic)
Recruitment Management Software Benefits (Infographic)
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalm
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a series
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - Infographic
 

ASP.NET-MVC-Part-1.ppt

  • 1. ASP.NET MVC Part I Ventsislav Popov Developer Evangelist at Microsoft ventsypopov.com ventsypopov.com
  • 2. Agenda  Beforehand – ASP.NET Web Forms  What is MVC  What is ASP.NET MVC?  Models  Views  Controllers  Validation  Routing  UnitTests  View engines 2 ventsypopov.com
  • 3. ASP.NET Web Forms  Rich controls and tools  Postbacks  Event driven web development  Viewstate  Less control over the HTML  Hard to test  Rapid development 3 ventsypopov.com
  • 4. Let’s chat for a bit… 4 ventsypopov.com
  • 6. Model –View - Controller 6  Controller - responsible for handling all user input  Model - represents the logic of the application  View - the visual representation of the model ventsypopov.com
  • 7. ASP.NET MVC  More control over HTML  No Codebehind  Separation of concerns  Easy to test  URL routing  No postbacks  NoViewState 7 ventsypopov.com
  • 8. Models  The model should contain all of the application business logic, validation logic, and database access logic.  ASP.NET MVC is compatible with any data access technology (for example LINQ to SQL)  All .edmx files, .dbml files etc. are located in the Models folder. 8 ventsypopov.com
  • 9. Custom View Models 9  When you combine properties to display on a View namespace ContosoUniversity.ViewModels { public class AssignedCourseData { public int CourseID { get; set; } public string Title { get; set; } public bool Assigned { get; set; } } } ventsypopov.com
  • 10. Creating a Model - DEMO 10 ventsypopov.com
  • 11. What is Controller?  It is a class  Derives from the base System.Web.Mvc.Controller class  Generates the response to the browser request 11 public class HomeController : Controller { public ActionResult Index() { ViewBag.Message = "Welcome to ASP.NET MVC!"; return View(); } public ActionResult About() { return View(); } } ventsypopov.com
  • 12. Controller Actions  Public method of the Controller class  Cannot be overloaded  Cannot be a static method  Returns action result 12 public ActionResult About() { return View(); } ventsypopov.com
  • 13. Action Results  Controller action response to a browser request  Inherits from the base ActionResult class  Different results types 13 ventsypopov.com
  • 14. Implement a Controller - DEMO 14 ventsypopov.com
  • 15. Action Results Types  ViewResult  EmptyResult  RedirectResult  JsonResult  JavaScriptResult  ContentResult  FileContentResult  FileStreamResult  FilePathResult 15 ventsypopov.com
  • 16. Controller base class methods  View  Redirect  RedirectToAction  RedirectToRoute  Json  JavaScriptResult  Content  File 16 ventsypopov.com
  • 17. Views  Most of the Controller Actions return views  The path to the view is inferred from the name of the controller and the name of the controller action.  ViewsControllerNameControllerAction.aspx  A view is a standard (X)HTML document that can contain scripts.  script delimiters <% and %> in the views 17 ventsypopov.com
  • 18. Pass Data to a View  WithViewData:  ViewData["message"] = "Hello World!";  Strongly typedViewData:  ViewData.Model = OurModel;  WithViewBag:  ViewBag.Message = "Hello World!"; 18 ventsypopov.com
  • 19. Post data to a controller  Verb Attributes  The action method in the controller accepts the values posted from the view.  The view form fields must match the same names in the controller. 19 ventsypopov.com [HttpPost] public ActionResult Edit(Movie movie) { if (ModelState.IsValid) { db.Entry(movie).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } return View(movie); }
  • 20. Explore a View - DEMO 20 ventsypopov.com
  • 21. HTML Helpers  Methods which typically return string.  Used to generate standard HTML elements  textboxes, dropdown lists, links etc.  Example: Html.TextBox() method  Usage is optional  You can create your own HTML Helpers 21 ventsypopov.com
  • 22. Validation  Two types of validation error messages  generated before the HTML form fields are bound to a class  generated after the form fields are bound to the class  Model State  Validation Helpers  Html.ValidationMessage()  Html.ValidationSummary() 22 ventsypopov.com
  • 24. Routing  The Routing module is responsible for mapping incoming browser requests to particular MVC controller actions.  Two places to setup:  Web.config file  Global.asax file 24 ventsypopov.com
  • 25. Routing Setup  Web.config file 25 <system.web> <httpModules> … <system.web> <httpHandlers> … <system.webServer> <modules> … <system.webServer> <handlers> … ventsypopov.com
  • 26. Routing Setup Global.asax file 26 public class MvcApplication : System.Web.HttpApplication { public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( "Default", "{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = "" } ); } protected void Application_Start() { RegisterRoutes(RouteTable.Routes); } } ventsypopov.com
  • 27. URL Example http://www.mysite.com/Home/About/6 {controller} = Home {action} = About {id} = 6 27 ventsypopov.com ventsypopov.com
  • 28. Routing example - DEMO 28 ventsypopov.com
  • 29. Unit Tests  Used for the business logic (not DAL orView logic).  Test individual “unit”of code  Make the code safe to modify  Mock Object framework  When you lack “real” objects  Create mocks for the classes in the application  Test with mock objects 29 ventsypopov.com
  • 30. Unit Tests - DEMO 30 ventsypopov.com
  • 31. View Engines  Handles the rendering of the view to UI (html/xml);  Different view engines have different syntax  ASP.NET MVC 3 Pre-includedView Engines:  Web Forms  Razor 31 ventsypopov.com
  • 32. View Engines - DEMO 32 ventsypopov.com
  • 33. Things to remember  What MVC stands for  How ASP.NET MVC differs from Web Forms  Where is routing configured  How to validate business logic  How to use helpers  Unit tests basics  Choice between “View Engines” 33 ventsypopov.com
  • 34. Useful sites  http://www.asp.net/mvc  http://msdn.microsoft.com/en- us/library/dd394709.aspx  http://stackoverflow.com/  http://jquery.com/ 34 ventsypopov.com
  • 35. Questions? ASP.NET MVC ventsypopov.com Email: vepopov [at] microsoft.com Twitter: @v_popov
  • 36. Time to wake up :) 36