ASP.NET MVC Tip: Add a new T4 template for making MVCContrib Grid Helper Component

In this tip, I demonstrate how you can add a T4 scaffolding template within the “Add View” dialog of the ASP.NET MVC Framework. I am creating a List scaffolding template for MVCContrib Grid helper component. Earlier, I have blogged about how to use MVCContrib Grid helper component but recently the component has changed a lot and introducing a fluent interface  that provides a cleaner and more discoverable API. You can read all details from Jeremy Skinner's blog about the new enhancements of MVCContrib Grid helper component. I am creating the T4 template against the latest trunk of MVCContrib Grid helper.

Overriding default T4 templates of ASP.NET MVC

The default behaviour of T4 templates available in the  “AddController” and “AddView” dialog is machine-level. You can override the default T4 templates of ASP.NET MVC framework at per project level and also able to add additional templates within the “Add Controller” and “Add View” dialog. if you want override the global T4 tempates at project level, just copy the  C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\ItemTemplates\CSharp\Web\MVC\CodeTemplates” folder into your ASP.NET MVC project. When you add T4 templates at project level, you should set the “Custom Tool” property of each of the T4 template files to an empty string value. Otherwise Visual Studio will try to transform the template so that you will get an error. The CodeTemplates folder contains two subfolders AddController and AddView and you can add new templates into this folders. If you add a new T4 template under the AddView folder, the new template will show in the "Add View" dialog. In this demo, I am adding a new template named MvcContribList.tt. For adding a new template within the Visual Studio, just add a text file and rename it with .tt extension.  

AddView Dialog

 The Add View dialog shows the new added template MvcContribList in the View content list.

 

I have taken the default List.tt template and modified it for creating the new template for working with MVCContrib Grid helper component. I have added the below code to working with MVCContrib Grid helper.

   59 <%= Html.Grid< <#= mvcHost.ViewDataType #> >(Model).Columns(column => {

   60  <#

   61     foreach(string propertyName in properties) {

   62  #>

   63 column.For(x => x. <#= propertyName #> ); <# } #>

   64 column.For(x => Html.ActionLink("Edit", "Edit", new { id = x.ID })).DoNotEncode();

   65 column.For(x => Html.ActionLink("Delete", "Delete", new { id = x.ID })).DoNotEncode();  

   66 })   

   67 .Attributes(style => "width:100%")

   68 .Empty("There are no records")

   69   %>

   70   <p>

   71     <%= Html.ActionLink("Create New", "Create") %>

   72    </p>

   73 <#

   74 }

   75 #>

 

The source code along with a sample project available from here

Summary

In this tip, I demonstrated how to customizing and adding T4 templates and created a new T4 template for working with MVCContrib Grid helper component. You can easily override the global T4 templates of ASP.NET MVC framework and also able to add additional templates within the “Add Controller” and “Add View” dialog.

Published Wednesday, March 04, 2009 12:20 AM by shiju
Filed under: , ,

Comments

# ASP.NET MVC Tip: Add a new T4 template for making MVCContrib Grid Helper Component

Wednesday, March 04, 2009 2:12 AM by DotNetShoutout

Thank you for submitting this cool story - Trackback from DotNetShoutout

# ASP.NET MVC Tip: Add a new T4 template for making MVCContrib Grid Help

Wednesday, March 04, 2009 2:20 AM by DotNetKicks.com

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# re: ASP.NET MVC Tip: Add a new T4 template for making MVCContrib Grid Helper Component

Wednesday, March 04, 2009 3:11 AM by Raj

Is this template working as default List template?

# re: ASP.NET MVC Tip: Add a new T4 template for making MVCContrib Grid Helper Component

Wednesday, March 04, 2009 3:14 AM by shiju

@Raj

This template is working exactly same as the way of default List template.

# Reflective Perspective - Chris Alcock &raquo; The Morning Brew #299

Pingback from  Reflective Perspective - Chris Alcock  &raquo; The Morning Brew #299

# re: ASP.NET MVC Tip: Add a new T4 template for making MVCContrib Grid Helper Component

Wednesday, March 04, 2009 9:10 AM by Eric Hexter

Do you mind if we pull this into the Mvc Contrib project?  

# re: ASP.NET MVC Tip: Add a new T4 template for making MVCContrib Grid Helper Component

Wednesday, March 04, 2009 9:39 AM by Sarah

I recently came across your blog and have been reading along. I thought I would leave my first comment. I don't know what to say except that I have enjoyed reading. Nice blog. I will keep visiting this blog very often.

Sarah

www.craigslistdecoded.info

# ASP.NET MVC Tip: Add a new T4 template for making MVCContrib Grid Helper Component - Shiju Varghese's Blog

DotNetBurner.com - news and articles about .net DotNetBurner

# re: ASP.NET MVC Tip: Add a new T4 template for making MVCContrib Grid Helper Component

Wednesday, March 04, 2009 11:16 PM by shiju

Hi Eric Hexter,

It will be very happy if this is pull into the Mvc Contrib project.

# re: ASP.NET MVC Tip: Add a new T4 template for making MVCContrib Grid Helper Component

Thursday, March 05, 2009 12:49 AM by Steve

Shiju, great stuff. Thanks for the post

# ASP.NET MVC Archived Blog Posts, Page 1

Friday, March 06, 2009 12:41 AM by ASP.NET MVC Archived Blog Posts, Page 1

Pingback from  ASP.NET MVC Archived Blog Posts, Page 1

# ASP.NET MVC Tip: Add a new T4 template for making MVCContrib Grid &#8230;

Pingback from  ASP.NET MVC Tip: Add a new T4 template for making MVCContrib Grid &#8230;

# Link Listing - March 8, 2009

Monday, March 09, 2009 1:02 AM by Christopher Steen

Link Listing - March 8, 2009

# re: ASP.NET MVC Tip: Add a new T4 template for making MVCContrib Grid Helper Component

Wednesday, March 11, 2009 9:41 AM by Franitsek Kaduk

Another T4 template for extended MVCContrib Grid

# re: ASP.NET MVC Tip: Add a new T4 template for making MVCContrib Grid Helper Component

Thursday, April 09, 2009 1:20 PM by icnocop

These T4 templates need to be updated to support v1.0 rtm.

See this link for changes from RC1 to RTM:

blogs.msdn.com/.../asp-net-mvc-1-0-live.aspx

Never-the-less, thank you for sharing! :)

# ASP.NET MVC Tip: Add a new T4 template for making MVCContrib Grid &#8230; &laquo; ???????????????????????????????????????

Pingback from  ASP.NET MVC Tip: Add a new T4 template for making MVCContrib Grid &#8230; &laquo;  ???????????????????????????????????????

# re: ASP.NET MVC Tip: Add a new T4 template for making MVCContrib Grid Helper Component

Monday, July 05, 2010 2:35 AM by guess men's watches

thanks a lot

# re: ASP.NET MVC Tip: Add a new T4 template for making MVCContrib Grid Helper Component

Tuesday, September 28, 2010 9:32 AM by Hamed

How can I add paging, sorting and search functionality to this grid

# re: ASP.NET MVC Tip: Add a new T4 template for making MVCContrib Grid Helper Component

Friday, May 27, 2011 3:50 PM by weblogs.asp.net

Asp net mvc tip add a new t4 template for making mvccontrib grid helper component.. Nice :)

# ASP.NET MVC Tip: Add a new T4 template for making MVCContrib Grid &#8230; | ?????????????????????????????????

Pingback from  ASP.NET MVC Tip: Add a new T4 template for making MVCContrib Grid &#8230; | ?????????????????????????????????

Leave a Comment

(required) 
(required) 
(optional)
(required)