In the section “Specifying a View,” you learned how a controller specifies a view. But how does that view get created in the first place? You could certainly create a file by hand and add it to your Views directory, but the ASP.NET MVC tooling for Visual Studio makes it very easy to add a view using the Add View dialog.
Understanding the Add View Dialog Options
For this example, you'll add a new action method named Edit and then create a view for that action using the Add View dialog. To launch this dialog, right-click within an action method and select Add View (see Figure 3.2).
This brings up the Add View dialog shown in Figure 3.3. The following list describes each menu item in detail:
When launching this dialog from the context of an action method, the view name is prepopulated using the name of the action method. Naturally, the view name is required.
Scaffold | Description |
Empty | Creates an empty view. Only the model type is specified using the @model syntax. |
Create | Creates a view with a form for creating new instances of the model. Generates a label and editor for each property of the model type. |
Delete | Creates a view with a form for deleting existing instances of the model. Displays a label and the current value for each property of the model. |
Details | Creates a view that displays a label and the value for each property of the model type. |
Edit | Creates a view with a form for editing existing instances of the model. Generates a label and editor for each property of the model type. |
List | Creates a view with a table of model instances. Generates a column for each property of the model type. Make sure to pass an IEnumerable<YourModelType> to this view from your action method. The view also contains links to actions for performing the create/edit/delete operations. |
When creating a view that will contain a data entry form, such as an Edit view or a Create view, checking this option ensures that the generated view does reference these libraries. These libraries are necessary for implementing client-side validation. In all other cases, this checkbox is completely ignored.
Note that for custom view scaffold templates and other view engines, the behavior of this checkbox may vary as it's entirely controlled by the particular view scaffold T4 template.
Customizing the T4 View Templates
As mentioned earlier, when creating a strongly-typed view, you can select a view scaffold to quickly generate a particular type of view for the model.
The list of scaffolds shown in Table 3.1 is populated by the set of T4 templates located in the following directory depending on your Visual Studio install directory and the language of the scaffold you care about:
[Visual Studio Install Directory]Common7IDEItemTemplates[CSharp | VisualBasic]WebMVC 3CodeTemplatesAddViewCSHTML
On my machine, this is located at:
C:Program Files (x86)Microsoft Visual Studio 10.0Common7IDEItemTemplatesCSharpWebMVC 3CodeTemplatesAddViewCSHTML
This directory contains a .tt file for each view scaffold as shown in Figure 3.4.
You can modify these T4 files to your heart's content. You can also create new ones and they'll show up in the view scaffold drop-down list.
In general though, you might not want to change these files because they affect every project on your machine. Instead you have the option to customize these files per project by copying them into your project.
The easiest way to do this is to take the CodeTemplates folder and copy it directly into the root of your ASP.NET MVC 3 project. You'll want to delete any templates you don't plan to override.
Visual Studio will complain with the following message:
Compiling transformation: The type or namespace name ‘MvcTextTemplateHost’ could not be found (are you missing a using directive or an assembly reference?)
The reason for this is that when adding a T4 file to a project, Visual Studio sets the value of the Custom Tool property for each template to the value TextTemplatingFileGenerator. For a standalone T4 file, this is what you want. But in the case of your view scaffolds, this value is not correct. To fix this issue, select all of the T4 files and clear the Custom Tool property in the Properties window as shown in Figure 3.5.
The Add View dialog will now give preference to the view scaffold T4 templates in your project over the default ones of the same name. You can also give some templates a new name and you'll see the Add View dialog will show your new templates as options in the Scaffold Template drop-down list.