Getting Started With MVC 5 and Visual Studio 2013: @trobbins
Getting Started With MVC 5 and Visual Studio 2013: @trobbins
Getting Started With MVC 5 and Visual Studio 2013: @trobbins
ASP.NET
• A way to host .NET
application in IIS that let’s
you interact with HTTP
requests and responses
.NET
• A multi-language managed
code platform
Some problems with ASP.NET Web Forms
• Presented by Trygve
Reenskaug in 1979
• First used in the
Smalltalk-80 framework
– Used in making Apple
interfaces (Lisa and
Macintosh)
Step by Step
The Controller asks
the Model for data
The request hits the
controller
2 Model
1 3
B
Controller The Model gives the data
r back to the Controller
o
The controller formats the
w data and passes them to the 4
s View View
e
5
r
MVC WebForms
• Easier to Manage • Preservers State over HTTP
Complexity • Page Controller Pattern
• Does not use view state or • View state or server based
server based forms forms
• Rich Routing Structure • Works well for small teams
• Support for Test-Driven • Development is less
Development complex
• Supports Large Teams Well
The beauty of MVC
It’s Restful!
MVC Routes
With ViewData:
ViewData["message"] = "Hello World!";
Strongly typed ViewData:
ViewData.Model = OurModel;
With ViewBag:
• 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.
[HttpPost]
public ActionResult Edit(Meeting meeting)
{
if (ModelState.IsValid)
{
db.Entry(movie).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(meeting);
}
Controller
What is a controller
• It’s a class derived from
System.Web.MVC.Controller class
• Generate the response to the browser request
public class HomeController : Controller
{
public ActionResult Index()
{
ViewBag.Message = "Welcome to ASP.NET MVC!";
return View();
}
public ActionResult About()
{
return View();
}
}
Controller actions
Public method of the Controller class
Cannot be overloaded
Cannot be a static method
Returns action result
public class HomeController : Controller
{
public ActionResult Index()
{
ViewBag.Message = "Welcome to ASP.NET MVC!";
return View();
}
public ActionResult About()
{
return View();
}
}
Other Features
• Scaffolding
• Test Driven Development
• Internationalization
• Many More
MVC Best Practices
MVC best practice #1
Delete AccountController.cs
Why
• You will probably never use these account
management pages
• Keeping demo code in production
application is not a good practice
MVC best practice #2
Why
• Increases testability of your application
• Increases flexibility of your application
View best practice #3
When an ASP.NET MVC Web application runs in IIS 7.0, no file name
extension is required for MVC projects. However, in IIS 6.0, the
handler requires that you map the .mvc file name extension to the
ASP.NET ISAPI DLL.
MVC best practice #8
Pay attention to verbs
What happens when you refresh or submit a form?