Using view controllers

The ViewController class was added to simplify the view handling. It's just a bridge between the controllers and the views.

There are two kind of views in the framework:
  • Layouts renders the main layout (logo, menu etc)
  • method views (that renders the actual content).


Let's take a look at a controller that uses the haml views.

public class UserController : ViewController
{
  public string Hello()
  {
    return Render("user", Session["user"]);
  }
}


The render method takes arbitrary number of parameters. all parameters should be added in couples, where the first one is the name used in the view, and the second contains the actual object.
Parameters can also be added using the Arguments property.

public class UserController : ViewController
{
  public string Hello()
  {
    Arguments.Add("user", Session["user"]);
    return Render();
  }
}


I prefer using the first one when the number of parameters is small.

What does the view look like then?

%ul
  %li
    FirstName:
    = user.FirstName
  %li
    LastName:
    = user.LastName



Important The render method renders the method view inside the Layout automatically. This is NOT done for ajax requests, which makes it even simpler to add ajax support.

For instance, consider that you got the following view:

<a href="/user/hello">Will render into the layout</a>

<%= WebHelper.AjaxUpdater("/user/hello", "will render into the div below", "theDiv"); %>

<div id="theDiv"></div>


As you see, both links use the same url, but the returned content is different. The first link will display a complete page while the second will only fetch the method view and put it inside the div.

The WebHelper class is javascript library independent, although I've only added support for the prototype framework since it's the one I use myself.

Last edited May 22, 2008 at 9:23 AM by jgauffin, version 2

Comments

No comments yet.