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 (11)

  1. Ali says:

    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 .

  2. spark_guy says:

    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? :)

  3. davidebb says:

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

  4. spark_guy says:

    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

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

  6. Hi,

    Had a brain wave. I don’t have MVC 2 Beta installed to know for sure, but assuming the minor version is greater than MVC 2 Preview.

    I’ve made the following changes.

    Line 95:

    <# if (MvcVersion > 2) {#>

    Line 310:

    static float MvcVersion;

    Line 346:

    float GetMvcVersion() {

       var vsProject = (VSLangProj.VSProject)Project.Object;

       foreach (VSLangProj.Reference r in vsProject.References) {

           if (r.Name.Equals("System.Web.Mvc", StringComparison.OrdinalIgnoreCase)) {

               return r.MajorVersion + (r.MinorVersion / 10);

           }

       }

       // We should never get here, but default to v1 just in case

       return 1;

    }

  7. davidebb says:

    @Richard: I can see how your suggested change would be useful later if an MVC build with a non-zero minor version comes out, but right now the only existing versions are 1.0 and 2.0.  Can you clarify what specific issue you’re seeing?  Thanks!

  8. davidebb says:

    @Richard: oh I get it, you’re using the preview and not the newer beta. Unfortunately, they both have the same version, so that won’t work. Best thing to do is to move to the beta: http://www.microsoft.com/downloads/details.aspx?FamilyID=4817cdb2-88ea-4af4-a455-f06b4c90fd2c&displaylang=en

  9. Johan Driessen says:

    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.

  10. davidebb says:

    @Johan: ah right, forgot about that limitation.  The simplest workaround is to just delete those lines in the template.  Look for the two methods under ‘if (MvcVersion >= 2)’ around line 100.

  11. A shame about the minor version, hopefully it won’t be too long before VS2010 catches up.

    Thanks for your help,

    Rich