T4MVC 2.5.01: added support for Html.RenderAction and Html.Action

To get the latest build of T4MVC:

Go to T4MVC page on CodePlex

MVC 2 Beta introduces two nice helpers called Html.RenderAction and Html.Action.  Phil Haack described them in detail on his blog, so you may want to read through that before reading this post.

Basically, they’re two additional methods that follow the standard MVC pattern of passing the controller name and action name as literal strings, and the action parameters as anonymous objects.  e.g. Copying from Phil’s example, if you have an Action like this:

 public ActionResult Menu(MenuOptions options) {
    return PartialView(options);

You can write this in your View:

 <%= Html.Action("Menu", new { options = new MenuOptions { Width=400, Height=500} })%>

Note how the action name is hard coded, and the MenuOptions parameter is passed as an anonymous object.  With T4MVC, you can instead write:

 <%= Html.Action(MVC.Home.Menu(new MenuOptions { Width=400, Height=500})); %>

Giving you full intellisense and type checking for the controller name, action name and parameters.

Another advantage is that normally, if you use an ActionName attribute, you’ll need to pass that name instead of the action method name.  Again from Phil’s blog:

public ActionResult Menu(MenuOptions options) {
    return PartialView(options);

And you then have to pass “CoolMenu” instead of “Menu” when you make the call, which is pretty easy to get in trouble with.  But with T4MVC, the fact that you use a non-default Action Name is abstracted out, so you make the exact same Html.RenderAction(MVC.Home.Menu(...)) call.

Of course, all of this is nothing new if you’re used to using T4MVC.  It’s just the same pattern as we have everywhere else, applied to those couple new methods.  Maybe I’m just rehashing the same old benefits of strong typing and avoiding literal strings :)

On a separate note: I fixed a small bug that happened when you used a custom ActionResult without an explicit ctor.  See comments in previous post for details on the issue.