An editor template is a partial view file that provides the code to generate a portion of an overall view. In this case, the editor template is a C# razor file (.cshtml). The code in an editor template runs at application runtime on the web server to generate a portion of the HTML for the final web page.
All editor templates are located in an EditorTemplates subfolder. In particular, ASP.Net MVC automatically searches for editor templates in the following locations, in the order listed (relative to the MVC project root):
Areas\<areaname>\Views\<controllername>\EditorTemplates\<templatename>.cshtml
Areas\<areaname>\Views\Shared\EditorTemplates\<templatename>.cshtml
Views\<controllername>\EditorTemplates\<templatename>.cshtml
Views\Shared\EditorTemplates\<templatename>.cshtml
Where:
<areaname> is the name of the applicable area, if any.
<controllername> is the name of the controller.
<templatename> is the name of the editor template.
Note: An Area is a logical grouping of Controller, Models, Views and other related folders in an MVC application. Areas are an optional feature in ASP.Net MVC. For an example, refer to https://msdn.microsoft.com/en-us/library/ee671793(v=vs.100).aspx.
The Unisys-supplied editor templates are placed in the folder Views\Shared\EditorTemplates. Therefore, you can override an editor template by copying it from Views\Shared\EditorTemplates to one of the other folders in the search chain. For example, if you wanted to create your own customized version of the BootstrapFormGroup editor template, you would copy the file Views\Shared\EditorTemplates\BootstrapFormGroup.cshtml to the folder Views\<controllername>\EditorTemplates and edit the copy.
BootstrapForm Editor Template
The BootstrapForm editor template is the template responsible for generating a bootstrap form (refer to http://getbootstrap.com/css/#forms) for one model. The following example is an excerpt from this template (subject to change):
... foreach (string scaffoldProperty in GetEligibleProperties(Model.GetType(), String.Empty, Model.GetType().ToString())) { @Html.Editor(scaffoldProperty, "BootstrapFormGroup") ...
Therefore, this template automatically finds all properties that should be scaffolded and uses the BootstrapFormGroup editor template to generate the markup for each property.
BootstrapFormGroup Editor Template
The BootstrapFormGroup editor template is the template responsible for generating a bootstrap form group (refer to http://getbootstrap.com/css/#forms) for one particular model property. This template is invoked from the BootstrapForm editor template, as well as directly from view files generated by the Unisys View Model Form (Bootstrap Form Groups) template. Therefore, by default, this editor template is used by all views generated using either the Unisys View Model Form (Bootstrap Form) template or Unisys View Model Form (Bootstrap Form Groups) template.
The following example illustrates an excerpt from this template (subject to change):
... // determine UIHint string hint = ViewData.ModelMetadata.TemplateHint; if (hint == null) { hint = ""; } switch (hint.ToLower()) { case "datasource": break; case "alertbox": case "hidden": @Html.Partial("EditorTemplates/" + hint) break; case "checkbox": <div class="form-group"> <div class="col-md-offset-2 col-md-10"> @Html.Partial("EditorTemplates/" + hint) @Html.uuiLabel() </div> </div> break; case "checkboxlist": case "combobox": case "listbox": case "dropdownlist": case "image": case "radiobuttonlist": case "switch": case "textbox": case "barcodereader": case "contactaddress": case "contactemail": case "contactphone": <div class="form-group"> @Html.uuiLabel(new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.Partial("EditorTemplates/" + hint) @Html.ValidationMessage(@ViewData.ModelMetadata.PropertyName, new { data_valmsg_for = @Html.NameForModel() }) </div> </div> break; ...
This template looks at the UIHint and generates differently formatted bootstrap form groups, depending on the type of control. For example, a hidden field does not have a label; a checkbox has a label on the right; other controls have a label on the left and include client side validation.
UIHint Editor Templates
Unisys also supplies an editor template for each different possible setting of the UIHint annotation; therefore, there is an editor template for each possible ControlStyle that you can configure in an ePortal client message.
Refer to the following topics for more information:
The name of the editor template file is <uihintname>.chstml, where <uihintname> is the value of the UIHint attribute.
Each UIHint editor template is responsible for generating the edit control for a single style of control. Note that this template should not include generating a label or validation, just the edit control (such as a checkbox or textbox).
You can also create your own custom controls by supplying a custom UIHint value and writing your own corresponding editor template.
Creating Custom Editor Templates
You can create a custom editor template to change the code that is generated when you run your application. The easiest way to do this is to copy one of the existing Unisys editor templates and make modifications as needed.
Note: Do not edit the Unisys-supplied editor template files directly; instead make a copy of the .cshtml file and edit the copy. Unisys might provide future updates to the NuGet packages that contain the editor templates. If you install these updated NuGet packages, the existing Unisys-supplied editor template files will be overwritten, and any changes will be lost.
You might want to use a custom editor template in the following cases.
Updating the Code Generation for an Existing Editor Template
To change one of the existing Unisys-supplied editor templates, do the following:
In Solution Explorer, navigate to the folder containing the editor template that you wish to copy (Views\Shared\EditorTemplates).
Right-click the .cshtml file and select Copy.
In Solution Explorer, navigate to one of the other folders higher in the editor template search chain, such as Views\<controllername>\EditorTemplates, where <controllername> is the name of your controller.
Right-click the folder and click Paste.
Double-click the new .cshtml file to open it in the editor, then edit the new .cshtml file as needed. Refer to Editing .cshtml Files for more information.
Creating a New Editor Template for a Custom Control
When you edit client messages, you specify the ControlStyle. If none of the ControlStyle choices that Unisys supplies fit your needs, you can define your own custom control by selecting Custom for ControlStyle and providing a UIHint value that matches your custom control name. Refer to Controlling the Look and Feel in All Data Source Projects for more information.
To create a new custom control based on a Unisys-supplied editor template, do the following:
In Solution Explorer, navigate to the folder containing the editor template you wish to copy (Views\Shared\EditorTemplates).
Right-click the .cshtml file that most closely matches your needs and select Copy.
In Solution Explorer, navigate to any of the folders in the editor template search chain, depending on your needs. You can simply choose Views\Shared\EditorTemplates if your custom control will be shared by all controllers.
Right-click the folder and click Paste.
Rename the new file to the name of your custom control (for example, MyCustomControl.cshtml). The name before the .cshtml extension must match the value you specified for the UIHint in the client message.
Double-click the new .cshtml file to open it in the editor, and then edit the new .cshtml file as needed. Refer to Editing .cshtml Files for more information.
Refer to MVC Scaffolding and Templating Examples for examples.