RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


MVP Corner: Use the ASP.NET MVC Framework to Write Web Apps without ViewState or Postbacks : Page 3

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.

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).

   --/Controllers/RsvpController.cs File
   public class RsvpController : Controller
      public void Index()
For example's sake, I'll change the URL to be http://www.partywithpalermo.com/rsvp/list where list will be the action.

   --/Controllers/RsvpController.cs File
   public class RsvpController : Controller
   private IAttendeesRepository repository
      = new AttendeeRepository();
      public void List()
         IEnumerable<Attendee> attendees =
         RenderView("RsvpList", attendees);
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.

   --/Views/RsvpList.aspx File
   <%@ Page Language="C#" 
     RsvpHome" %>
   <%@ Import namespace="PartyWithPalermo.
   <%@ Import namespace="PartyWithPalermo.
     "-//W3C//DTD XHTML 1.0 
   <html xmlns="http://www.w3.org/1999/xhtml" >
     <head runat="server">
       <title>Party with Palermo</title>
       <table border="1">
        <% foreach (Attendee attendee in 
              ViewData) { %>
                <td><%=attendee.Name %></td>
                <td><%=attendee.Website %></td>
                <td><%=attendee.Comment %></td>
        <% } %>
   --/Views/RsvpList.aspx.cs File
   public partial class RsvpHome : 
      protected void Page_Load(object sender,
         EventArgs e)
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.


Web site


Jeffrey Palermo


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

Homer Simpson



Bart Simpson


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.

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.

Jeffrey Palermo is a software management consultant and the CTO of Headspring Systems in Austin, TX. His company specializes in Agile coaching and helping other companies double the productivity of software teams. Jeffrey is a MCSD.NET, Microsoft MVP, Certified Scrummaster, Austin .Net User Group leader, AgileAustin board member, INETA speaker, INETA Membership Mentor, Christian, husband, father, motorcyclist, Eagle Scout, U.S. Army Veteran, and Texas A&M University graduate. Headspring Systems is available for consulting.
Email AuthorEmail Author
Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date