T4MVC 2.6.20, and upcoming T4MVC talk at MvcConf

I just pushed T4MVC out to the MvcContrib CodePlex site.  You can go to the T4MVC Home Page to get started with it.

Last time I blogged about a T4MVC release was for 2.6.13.  In the mean time, I released 2.6.14 and 2.6.15, but they were minor updates so I just tweeted them.  You can check out the history page to see what they changed.  Now, 2.5.20 brings a more interesting new feature, so I figured I’d blog it.

New feature to easily render Partials


Update (7/20/2010): there was a small issue with this change when multiple ascx have the same name.  I just pushed a new 2.6.21 build which addresses it.

This new feature was written by Evan Nagle (who is also the author of the Chirpy add-in I discussed a few days ago).  You can check out the post he wrote about it.  In a nutshell, it’s about making it easier to render partial pages.  e.g. suppose you have a partial page called MyUC.ascx (say in Shared) and want to render it from another page (and pass some view data).  Normally, you would write something like this:

<% Html.RenderPartial("MyUC", data); %>

Here, “MyUC” is a magic string, and surely it has to go!  T4MVC has always provided a way to do this:

<% Html.RenderPartial(MVC.Shared.Views.MyUC, data); %>

Basically, it uses the T4MVC generated view token to avoid using a literal string.  Functional, but ugly!  But now, with this new feature you can simply write:

<% Html.RenderMyUC(data); %>

Here, T4MVC generated an extension method specifically to render the partial page, making the code not only free of magic strings, but also more concise and readable.

To disable this functionality: you can turn this off by setting ExplicitHtmlHelpersForPartials to false in T4MVC.settings.t4


T4MVC talk at MvcConf this Thursday (7/22)

Update (7/30/2010): I ended up doing a dual talk on the Razor view engine and T4MVC.  It was a last minute change since we didn’t have a speaker for the Razor talk.  It was a bit of a scramble to get that in, but the talk went well, and is available for all to watch here.


The other thing I wanted to discuss is the T4MVC talk that I’ll be giving at the MvcConf virtual conference.  The talk will be this Thursday at 12pm Pacific (2pm Central).  Check out the full schedule here.  Here is the talk abstract:

Name: T4MVC – the magic string killer

Abstract: Magic strings occur frequently in MVC when referring to Controllers, Actions and Views, and lead to code that can be hard to validate and maintain. In this session, I will show you how to use T4MVC (part of MvcContrib) to almost completely obliterate those magic strings.  To make this a bit different from my previous sessions on this topic, I’ll do the demos by starting with a new MVC app from scratch, instead of starting from a complete sample like NerdDinner.  I’ll be running MVC2 on VS 2010, and will include discussion of new MVC features like Areas.  I’ll also present a VS Add-In that can be used to automatically run T4MVC.  Finally, I will discuss how T4MVC can be used with the upcoming Razor view engine.

Hopefully I’ll see you all there, at least virtually 🙂

Note: even though the MvcConf site says that the conference is Sold Out, Eric is trying to find a way to stream it more globally (e.g. via ustream).  Check the MvcConf site for updates on this.

Comments (12)

  1. Daniel says:

    I tried the new version but it doesn't work for me. I get an error that there is duplicated keys in the dictionary that is supposed to contain the partial views.

  2. Mohamed says:

    Is there an easy switch to NOT generate the RenderMyUC() style methods?

    When the website grow, I think this will pollute the intellinse with so many methods!

    Render(My.Typed.Path.To.View) is really just fine

  3. David Ebbo says:

    @Daniel: yes, there is an issue.  Investigating…

    @Mohamed: you can turn this off by setting ExplicitHtmlHelpersForPartials to false in T4MVC.settings.t4

  4. David Ebbo says:

    @Daniel: please try 2.6.21 which has the fix.

  5. Daniel says:

    It works now!

    However I have another problem with [DefaultValue] attributes.

    I have a action that looks like this:

           public virtual ActionResult Index(

               OrderBy orderBy,

               [DefaultValue(SearchResultView.BasicView)] SearchResultView view,

               CustomerSearchQuery query)        

    Here SearchResultView is an enum, and it worked before using T4MVC but now it will not get bound and the action throws an exception about missing the parameter "view".

  6. Jose says:

    Hi David. Congratulations for the excelent work.

    Why not include in the T4 the ability to add a random or timestamp based suffix to the static resources for caching purposes? For example, you could still use the <img src="<%= Links.Content.nerd_jpg %>" /> to generate <img src="/Content/nerd.jpg" />, but you could instead generate <img src="/Content/nerd.jpg?201007071200234" /> where 201007071200234 is the timestamp of the last change of the file. This would invalidate any browser cache related to this particular image.

    Obviously, this could be the default behaviour, a configuration or an alternate property (Links.Content.nerd_jpg_with_timestamp instead of Links.Content.nerd_jpg).

    What do you think? Its really easy to implement and very useful, don't you think?

  7. Mohamed says:

    Thanks a lot 🙂

  8. David Ebbo says:

    @Daniel: I can't repro this DefaultValue issue.  Are you sure that it is related to T4MVC, and not some unrelated thing going on in your app?  If you can put together a minimal repro (e.g. starting with an empty app), you can email it to me and I'll be glad to investigate.

  9. David Ebbo says:

    @Jose: actually, this is already supported, but it's turned off by default.  Try setting AddTimestampToStaticLinks=true in the settings file

  10. Ryan Heath says:

    First I'd like to say thank you for creating T4MVC. I like it a lot  🙂

    Second, how about also adding


    to a specific controller ( I think you can get away with an extention method too)

    so we do not have to do

    return View(MVC.SpecificController.Views.MyUC, data);


    return ViewMyUC(data);

    in the controller.

    // Ryan

  11. David Ebbo says:

    @Ryan: doing something like this is possible.  But note that there is already a shortcut that you may not be aware of.  If you're going to a view that's not in a different controller, you can simply write

       return View(Views.MyUC, data);

    instead of

       return View(MVC.SpecificController.Views.MyUC, data);

    Which is pretty concise,

  12. Ryan Heath says:

    Ah thanks! How did I missed that?!

    // Ryan