Asp.Net MVC programming advice. Toodles, Evan Nagle.

T4MVC

Thanks to David Ebbo’s support, I’ve been adding Explicit Helper functionality to the T4MVC portion of the MvcContrib project. Below, I’ll explain the changes I’ve requested/published in MvcContrib, and how you (lucky fellow) can use the Explicit Helper functionality in your MVC projects, too.

Before I go any further, make sure to download the newest version of MvcContrib from The MvcContrib CodePlex Page. You’ll find the T4MVC folder in the extracted MvcContrib folder. I’ll show you what to do with the T4MVC files in just a second.

I also suggest that you follow the directions on Wayne Brantley’s blog to install the T4MVC VS AddIn. This AddIn auto-updates the T4 template. Without the AddIn, you’ll have to use some trickery (or some right clicking) to keep the T4 template up-to-date.

Once you’ve downloaded T4MVC, create a folder in your project called “T4MVC” or “T4MVC Files”. Redundant, I know, but it’ll be helpful, I promise:

T4MVC Folder T4MVC

T4MVC Folder

Back in the MvcContrib folder, grab the T4MVC.tt and T4MVC.settings.t4 files and throw them into the “T4MVC Files” folder that you just created. For good measure, I also threw in the readme.txt:

T4MVC Files T4MVC

T4MVC Files

Right click on T4MVC.tt and select Run Custom Tool. Bamboozled! A bunch of fancy-shmancy code has been generated for you. Pretty sweet, and super-easy:

T4MVC Generated Files T4MVC

T4MVC Generated Files

My piece of the puzzle can be found in the file, T4MVC.ExplicitExtensions.cs. This is where your new T4MVC Explicit Extensions live. Below, I’ll list out the different types of helpers I’m working on, their status of inclusion in MvcContrib, and how you, as a T4MVC fanatic, will be able to use them to simplify your MVC code.

Extension 1: Explicit Rendering of Partials

Status: included in MvcContrib
Initial Blog: here

In my project, I have only one partial, the LogOnUserControl partial:

LogOnPartial T4MVC

Log On Partial


With T4MVC in my project, I can now render the partial thusly:


<!-- pass no model: -->
<% Html.RenderLogOnControl(); %>

<!-- pass a model -->
<% Html.RenderLogOnControl(model); %>

If you don’t like the “Render{0}” format for Explicit Partial Rendering methods, no worries. You can change the format to whatever you like. In T4MVC.settings.t4, modify the value of the ExplicitHtmlHelpersForPartialsFormat variable. For example, I could change the format to:


//create explicit HtmlHelpers for rendering partials
const bool ExplicitHtmlHelpersForPartials = true;
const string ExplicitHtmlHelpersForPartialsFormat = "Show{0}Now";

And, in my view, I can now render my partials like this:


<!-- pass no model: -->
<% Html.ShowLogOnControlNow(); %>

<!-- pass a model -->
<% Html.ShowLogOnControlNow(model); %>

Extension 2: Explicit Rendering of Html Forms

Status: pending review in MvcContrib
Initial Blog: here

In my project, I have an ActionResult method set to accept Postbacks:


public class Account : Controller
{
   [HttpPost]
   public virtual ActionResult Lump() {
      return null;
   }
}

With T4MVC in my project, I can now (pending review) render an html form like this:


<!-- unclosed: -->
<% Html.AccountLumpForm(); %>
    ...
<% Html.EndForm(); %>

<!-- closed: -->
<% using(Html.AccountLumpForm()) { %>
    ...
<% }; %>

<!-- html attributes: -->
<% Html.AccountLumpForm(new { @id = "lumpformid" }); %>
    ...
<% Html.EndForm(); %>

<!-- html attributes and route values: -->
<% Html.AccountLumpForm(new { @id = "lumpformid" }, new { id = 1}); %>
    ...
<% Html.EndForm(); %>

If you don’t like the “{controller}{action}Form” format for Explicit Form methods, no worries. You can change the format to whatever suits your fancy. In T4MVC.settings.t4, modify the value of the ExplicitHtmlHelpersForFormsFormat variable. For example, I could change the format to:


//create explicit HtmlHelpers for rendering html forms
const bool ExplicitHtmlHelpersForForms = true;
const string ExplicitHtmlHelpersForFormsFormat = "FormFor{0}{1}"; //0 = controller, 1 = action

And, in my view, I can now render my form like this:


<!-- pass no model: -->
<% Html.FormForAccountLump(); %>
<% Html.EndForm(); %>

More To Come

As I submit more Explicit T4MVC Helpers to MvcContrib, I’ll post the information here. Let me know what you think. Or, at the very least, keep keeping it really real…