What a T4 Template is in MVC

In this article we learn what a Text Template Transformation Toolkit (T4) template is and how to customize it.

What a T4 template is

We use this template to generate the code when we add a view or controller in MVC. The file extension of this template is tt. Basically when we add a view or controller using a scaffhold template it is called a T4 template. By using this template if we add a controller or view then it generates some code automatically.

Suppose we made a class file in the model folder named ListEmployee.cs as in the following:

  1. using System.ComponentModel.DataAnnotations.Schema;  
  2. using System.Data.Entity;  
  3.   
  4. namespace mvcDemo.Models  
  5. {  
  6.     [Table("Employee_Master")]  
  7.     public class ListEmployee  
  8.     {  
  9.         public int Id { getset; }  
  10.         public string Name { getset; }  
  11.         public string Email { getset; }  
  12.     }  
  13.   
  14.     public class ListEmployeeContext : DbContext  
  15.     {  
  16.       public  DbSet<ListEmployee> listEmployee { getset; }  
  17.     }  
  18. }  
Add a controller and name it EmployeeController. This will generate:

employee controller

When we click on the Add button it generates a controller that has index, details, create, edit and delete methods and in the View folder a folder named Employee was generated with 5 views containing the same name as above. This is called a T4 template.

You can find the T4 template in the following paths.

For Views:

C:\Program Files\Microsoft Visual Studio 11.0\Common7\IDE\ItemTemplates\CSharp\Web\MVC 4\CodeTemplates\AddView\CSHTML

view

For Controllers:

C:\Program Files\Microsoft Visual Studio 11.0\Common7\IDE\ItemTemplates\CSharp\Web\MVC 4\CodeTemplates\AddController.

for controller

Now I will add my own T4 template to an existing list of templates.
  1. Copy list.tt and paste it.
  2. Rename it List then copy to PramodList
  3. Let's make some changes in PramodList.tt, to do this let's open PramodList.tt in Notepad.
I don't want to create a link on my list template so I am deleting that HTML Helper code. I am making some style changes for the <th> tag and save it.
  1. <th style="color:red;">@Html.DisplayNameFor(model => model.<#= property.ValueExpression #></th>  
Now I am trying to add a new view using a T4 template. We can see PramodList in the scaffhold template.

listemployee view

Add the following code to HomeController.
  1. public ViewResult ListEmployee()  
  2. {  
  3.       List<ListEmployee> listEmployee = new List<ListEmployee>();  
  4.       listEmployee.Add(new ListEmployee() { Id = 1, Name = "Pramod", Email = "[email protected]" });  
  5.       listEmployee.Add(new ListEmployee() { Id = 2, Name = "Ravi", Email = "[email protected]" });  
  6.       listEmployee.Add(new ListEmployee() { Id = 3, Name = "Rahul", Email = "[email protected]" });  
  7.       listEmployee.Add(new ListEmployee() { Id = 4, Name = "Deepak", Email = "[email protected]" });  
  8.       return View(listEmployee);  
  9. }  
After that I am adding a view called ListEmployee using the PramodList template and run the application. Let's see the output.

new layout

See the output is expected. There is no href tag for creating the new employee and the heading of the list is in the red color.

By default Visual Studio makes these change for all projects. But I don't, to do that I want the changes for a specific project. To do that let's copy the CodeTemplates folder located at “C:\Program Files\Microsoft Visual Studio 11.0\Common7\IDE\ItemTemplates\CSharp\Web\MVC 4” and paste it into our project root folder and delete PramodList.tt from “C:\Program Files\Microsoft Visual Studio 11.0\Common7\IDE\ItemTemplates\CSharp\Web\MVC 4\CodeTemplates\AddView\CSHTML”.

After making these change let's add a view using the PramodList template and run the application and see the output.

employee list

The output is the same and this template is only for the current project.

 


Similar Articles