Sunteți pe pagina 1din 16

Module 4

Developing MVC Controllers

Module Overview
Implementing MVC Controllers Creating Action Methods

Lesson 1: Implementing MVC Controllers


What is an MVC Controller? Demonstration: Creating an MVC Controller

What is an Action Filter?


Demonstration: Using an Action Filter

What is an MVC Controller?


The ASP.NET MVC framework is a lightweight, highly testable presentation

framework that is integrated with the existing ASP.NET features

The MVC framework includes the following components:

Models

Views Views are the components that display the applications user interface (UI)

Controllers

Model objects are the parts of the application that implement the logic for the applications data domain

Controllers are the components that handle user interaction, work with the model, and select a view to render the UI

Demonstration: Creating an MVC Controller

Create a new ASP.NET MVC 2 Web Application project

Create a controller named BlogController

Add action methods for Create, Update, Delete and Details scenarios

What is an Action Filter?

Action filters are custom attributes that provide a declarative means to add pre-action and post-action behavior to controller action methods.

Authorization filters Make security decisions about whether to execute an action method. Action filters Wrap the action method execution. Result filters Wrap the ActionResult. Exception Filters Execute if there is an unhandled exception thrown in the action method.

Demonstration: Using an Action Filter


Open the MvcApplication1 solution Open the HomeController controller In the About action method, throw an exception Add a customErrors element to the Web.config file

Run the application

Display the About page

Lesson 2: Creating Action Methods


What are Action Methods? Calling an MVC View

Retrieving Data from the Request


Demonstration: Retrieving Data from the Request

What are Action Methods?

An action method is a public method of a controller that can have any number of parameters and typically returns an object of type ActionResult. Typically, it calls the view and passes data to the view via the ViewData object.

Action methods must be public.


Action methods cannot be static. Action methods cannot have unbounded generic type parameters.

Action methods cannot be overloaded based on parameters unless they are disambiguated with attributes such as NonActionAttribute or AcceptVerbsAttribute.

Calling an MVC View


The ActionResult class encapsulates the result of an action method.
ContentResult Returns a user-defined content type. EmptyResult Returns a null result. FileResult Returns a binary file. JavaScriptResult Returns JavaScript. JsonResult Returns a serialized Json object. PartialViewResult Renders a partial view. RedirectResult Redirects to another action method by using its URL. RedirectToRouteResult Redirects to another action method. ViewResult Renders a view. This class contains properties that identify the view to render, the name of the view, the name of the master view, view data, temporary data, and a collection for view engines for the application.

Retrieving Data from the Request


Using the Request object. Using the FormCollection. Using Model Binders.

[HttpPost] public ActionResult Create(Blog blog) { if (ModelState.IsValid) { // TODO: Add insert logic here }; return RedirectToAction("Index"); }

Demonstration: Retrieving Data from the Request


Open the DemoHello Project Retrieve Data from the RouteData Retrieve Data from the QueryString Retrieve Data from a TextBox Retrieve Data from the FormCollection

Lab: Developing MVC Controllers


Exercise 1: Creating an MVC Controller Exercise 2: Adding Code to List the Blog Entries

Exercise 3: Adding Code to Create a New Blog Entry


Exercise 4: Adding Code to Edit a Blog Entry Exercise 5: Adding Code to Delete a Blog Entry

Logon information

Virtual machine User name Password

10264A-GEN-DEV Student Pa$$w0rd

Estimated time: 60 minutes

Lab Scenario

Lab Review
Review Questions

How did you create the MVC controller?

How did you list the blog entries by Blogger?


How did you read the data in the Create action method that is handled by the HTTP Post? How did you use the TempData property?

Which action method used a FormCollection for one of its parameters?

Module Review and Takeaways


Review Questions Real-World Issues and Scenarios

Best Practices

S-ar putea să vă placă și