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:

using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity;

namespace mvcDemo.Models
{
    [Table("Employee_Master")]
    public class ListEmployee
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Email { get; set; }
    }

    public class ListEmployeeContext : DbContext
    {
        public DbSet<ListEmployee> listEmployee { get; set; }
    }
}

Add a controller and name it EmployeeController. This will generate:

employee%20controller

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%20controller

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.

<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%20view

Add the following code to HomeController.

public ViewResult ListEmployee()
{
    List<ListEmployee> listEmployee = new List<ListEmployee>();
    listEmployee.Add(new ListEmployee() { Id = 1, Name = "Pramod", Email = "pramod@yahoo.com" });
    listEmployee.Add(new ListEmployee() { Id = 2, Name = "Ravi", Email = "ravi@yahoo.com" });
    listEmployee.Add(new ListEmployee() { Id = 3, Name = "Rahul", Email = "rahul@yahoo.com" });
    listEmployee.Add(new ListEmployee() { Id = 4, Name = "Deepak", Email = "deepak@yahoo.com" });
    return View(listEmployee);
}

After that I am adding a view called ListEmployee using the PramodList template and run the application. Let’s see the output.

new%20layout

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.

new%20layout

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

I hope this article is helpful for you.

Thanks :)

Tags: , ,

  • sumanprkt

    Well explained