Finally, those who are comfortable with the stateless nature of web programming will be able to develop ASP.NET sites without the insulating layer that ASP.NET has always provided.

by Jeffrey Palermo

Feb 25, 2008

Page 3 of 3

Extending the Controller
A key change is that RSVPController inherits from the Controller base class. The base class performs the Execute() method and wires up actions. Using the ControllerActionAttribute declares that the Index() method should be exposed as an action (Index() is the default action).

Note the RenderView() method in the preceding code. The RsvpList string argument denotes the name of a view in the /Views folder in the web application. It will look for the file /Views/RsvpList.aspx, load it, and render it. To retrieve the Attendees the code uses an IAttendeesRepository. The page code sets a property bag called ViewData with the objects that need to go to the view. In this case there's a single object but you can set several because ViewData is an IDictionary unless overridden.

The only difference from the code-behind is that it inherits from ViewPage instead of merely Page. By declaring the generic form of ViewPage you have strongly-typed ViewData, and you can create a presentation DTO to hold any information that needs to be passed to the view. You'll get IntelliSense when binding objects to the view. Table 1 shows the view rendered to the browser.

Name

Web site

Comment

Jeffrey Palermo

http://www.jeffreypalermo.com

Please RSVP, and you'll be added to this list!

Homer Simpson

http://www.simpsons.com

Doh!

Bart Simpson

http://www.simpsons.com

Don't have a cow.

Table 1: View rendered to the browser.

Note how simplified this view is. This technique allows you to leverage the raw power of HTML (and still use server controls) without ViewState, Postbacks, or a server-side form wrapping the entire page.

Conclusion
This brief overview of Microsoft's new ASP.NET MVC framework barely scratches the surface of its capabilities. I did not cover mapping query string or form variables into controller actions, output caching, etc., but the framework has all these covered. The ASP.NET MVC framework also supports advanced scenarios, including Inversion of Control (IoC) container creation of controllers, and loading UserControls as a partial view. The framework is still in the prototype stage, but when it hits CTP status in a month or so, check out my blog for more information.