Managing changes can be difficult. The following topics provide some best practices for using MVC layouts, scaffold templates, and editor templates to reduce the impact to your projects when changes are required.
Best Practices: Layouts
Consider the following when using layouts:
Place custom content in layouts instead of directly editing razor views wherever possible. If you need to rescaffold your views, you will not lose custom changes that you placed in a layout file.
You can define multiple layouts and use a different layout for each different portion of your application. You can even define a different layout for each view, if needed.
The filename containing a layout page should start with an underscore (_) character. This ensures that the web site user cannot access the page directly.
You identify the particular layout to use when scaffolding your views; refer to Invoking Scaffolders for more information.
Alternatively, you can manually edit your view to specify the layout to use, as shown in the excerpt below:
@{ ViewBag.Title = @Html.DisplayNameForModel(); ViewData["ParentModel"] = Model; Layout = "~/Views/Shared/_MyLayout.cshtml"; }
As an alternative to explicitly specifying the layout to use in each view, you could edit _ViewStart.cshtml. This file is located in the Views folder, and contains code that runs at the start of each view. By default, it defines which layout to use.
For more information on layouts, refer to:
Search for the topic "Creating a Consistent Layout" at http://www.asp.net.
Best Practices: Scaffold Templates
Consider the following when using scaffold templates:
Use the scaffold template that generates the least amount of markup possible for your needs. That is, use Unisys View Model Form (Bootstrap Form) if possible; otherwise, use Unisys View Model Form (Bootstrap Form Groups) if possible. Use Unisys View Model Form (Bootstrap Form Controls) as a last resort.
You can use different scaffold templates for different views, if desired. For example, you might use Unisys View Model Form (Bootstrap Form) for most views Then, you might use the other scaffold templates for a few views where you need to perform special customizations.
You can create your own custom scaffold templates; to do so, do not edit the Unisys-provided scaffold templates. Instead, copy an existing scaffolder to a new file name in the same folder, as described in Creating Custom Scaffold Templates.
Refer to Scaffold Templates for more information.
Best Practices: Editor Templates
Consider the following when using editor templates:
You can create your own custom editor templates; refer to the topic Creating Custom Editor Templates in Editor Templates.
You can debug in editor templates, as follows:
In Solution Explorer, navigate to the folder containing the editor template. Refer to Editor Templates for the list of folders that can contain editor templates.
Double-click the .chstml file for the editor template that you wish to debug.
The file opens in the editor.
Click on the left margin to set a debugger breakpoint.
Start debugging your MVC application. Refer to Testing an MVC Web Application for more information.
Refer to Editor Templates for more information.