When will T4MVC support Razor with explicit HtmlHelpers for rendering partials?
Asked Answered
C

2

8

I've been using T4MVC for some time now and love the "explicit HtmlHelpers for rendering partials" feature, which by default is switched off. I am using T4MVC version 2.6.40.

I recently upgraded to MVC3 and noticed that no explicit HtmlHelpers are generated for Razor partials, so I looked at the source code of the T4MVC text template and found a method named "GetPartials" which has a line of code as folows:

var parts = GetControllers()
        .Select(m => m.ViewsFolder)
        .SelectMany(m => m.Views)
        .Where(m => m.Value.EndsWith(".ascx"));

So it is clear that Razor views are not supported.

I'd also like to mention that when running the T4 template (right-click > run custom tool) I get a compiler warning stating: "The C# 2.0 and C# 3.5 compilers are no longer supported. Templates will always be compiled with the version 4 compiler instead of 'v3.5' as specified."

This relates to line 18 where the template language attribute has a value of "C#v3.5". Why does it have to have an explicit version dependency? Can it not just be "C#"?

Apologies for asking two seperate questions in one post.

Counterforce answered 1/2, 2011 at 9:56 Comment(0)
J
11

I just released T4MVC 2.6.42 to address this. You can get it from Codeplex or from NuGet.

Note that in order to have a razor file be detected as a partial by T4MVC, its name needs to start with an underscore (e.g. _foo.cshtml). Without this restriction, we would end up creating helper methods for all views, which would pollute things and not add value. Note that prefixing partial Razor views with _ is generally recommended by the MVC team.

As for the warning, it's unrelated and is benign. To get rid of it, just change language="C#v3.5" to language="C#". I can't make that change in the official version as that would make it break when running on 3.5 (and I don't want to maintain two separate versions just for that).

Jacklyn answered 2/2, 2011 at 2:42 Comment(0)
R
2

The compiler warning you receive is just that, a warning. It is not preventing T4MVC from working.

As for supporting Razor, you've found the appropriate code in the template - simply modify it.

var parts = GetControllers()
        .Select(m => m.ViewsFolder)
        .SelectMany(m => m.Views)
        .Where(m => m.Value.EndsWith(".ascx") || m.Value.EndsWith(".cshtml") || m.Value.EndsWith(".vbhtml"));

I'd give that a try.

It's just a T4 Template, not magic. All it contains is simple c# code that gets project info from the Visual Studio environment and generates some fairly simple c# code.

Rascally answered 1/2, 2011 at 15:45 Comment(1)
Thanks. I know T4 Templates are not magic. I definitely could have made the change myself. I just didn't want to waste my time making a change that would just be overwritten the next time I update T4MVC. I wanted to bring it to David's attention so he could release a fix so that everyone could benefit. I've actually found another bug in T4MVC since this and I know what to do to fix it, but I want to become a contributor to the project, so I've just requested to become a contributor. I hope I get accepted.Counterforce

© 2022 - 2024 — McMap. All rights reserved.