Published on

Changing the templates used to generate MVC views

Authors
  • avatar
    Name
    Mike Barram
    Twitter

If you use Visual Studio to generate MVC views, it will use T4 templates. This is how to edit those templates.

Go to:

C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\Extensions\Microsoft\Web\Mvc\Scaffolding\Templates\MvcView

Make a backup copy of the folder (don’t put the backup inside the folder or VS will complain that it has multiple templates to choose from)

Then,  open all of the files (or at least all of the *.cs.t4) files) in a text editor.

You can now modify the templates. I like to:

  • add the class “btn-primary” to submit buttons on edit and create views
  • add the class”btn-warning” on delete views
  • add <thead></thead> and <tbody></tbody> tags to List.cs.t4 – why does the template not have semantic markup?
  •  add the class “edit-details-delete” to the last <th></th> in List.cs.t4 – this lets me use CSS to set the “action” column to a fixed width

NOTE: installing Visual Studio 2013 update 2 will overwrite your templates. The new templates have extra bootstrap classes but not the ones above. They also turn enums into dropdowns, so you’ll probably want to use the new templates rather than overwriting them with a backup.