Freigeben über


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:

 [ActionName("CoolMenu")]
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.

Comments

  • Anonymous
    November 20, 2009
    Although that was nothing new as you mentioned but this lead me to use T4MVC and that was so cool to see the red strings go away from my clean MVC codes .

  • Anonymous
    November 21, 2009
    Hi David,

  1. a newbie question (just before I dive into it), can this useful framework (for whatever it is;-) be used with spark engine? I'm using spark atm (for obvious reasons)
  2. any blog post where you would constantly redirect ppl asking WTHeck is this? :)
  • Anonymous
    November 22, 2009
    @spark_guy: I haven't played extensively with the Spark view engine, but I think that T4MVC should work with it just as well as it works with the aspx view engine.  If you hit specific issues, please let me know. YOu can find all the T4MVC posts at http://blogs.msdn.com/davidebb/archive/tags/T4MVC/, though ideally there would be a single posts that describes it all in one place (I'll try to do this).  Note that if you download T4MVC, the readme has good 'getting started' info that tells you what it's all about.

  • Anonymous
    November 22, 2009
    that's great news David, going to download and read text files. and thank you for your hard work, I wonder why MVC v2 didn't include this project in the core... all the best

  • Anonymous
    November 22, 2009
    This new update obviously breaks in MVC 2 Preview, which I'm having to use on VS2010. Is there some way to detect whether the underlying methods exist before adding the additional support? Rich

  • Anonymous
    November 22, 2009
    The comment has been removed

  • Anonymous
    November 23, 2009
    The comment has been removed

  • Anonymous
    November 23, 2009
    The comment has been removed

  • Anonymous
    November 23, 2009
    Unfortunately, VS2010 beta 2 won't support the beta, only Preview 2. So if you, like me, are using VS2010 for your ASP.NET MVC project, you're kind of stuck with Preview 2. Is there any way to get hold of the previous version of t4mvc (2.5.00), that are usable in VS2010? I can only seem to find 2.5.01 on Codeplex.

  • Anonymous
    November 23, 2009
    The comment has been removed

  • Anonymous
    November 23, 2009
    A shame about the minor version, hopefully it won't be too long before VS2010 catches up. Thanks for your help, Rich