What you call "Include", the name in ASP.NET MVC is Partial . Partials are code fragments (.cshtml extension, equal to Views ), and that nomenclature is usually normalized with an "underline" in front of the name.
Separation is not only good as it is encouraged. You do not have to worry about performance: because Views are compiled dynamically, the performance of a View with or without Partials p>
The tips are basically explanations of features:
- A Partial within a
Shared
directory is visible to all other Views ;
-
A Partial within a Views directory is usually visible only to those Views , but can be called with proper directory specification;
Example, suppose I am rendering ~/Views/Produtos/Detalhes.cshtml
and want to display more information about the product category:
@Html.RenderPartial("~/Views/CategoriasDeProdutos/_Detalhes.cshtml", produto);
Partials is a good practice;
-
Partials should not (and can not) have JavaScript code inside. The code should be in View parent, to avoid strange behavior.
-
There are two ways to call Partials :
@Html.RenderPartial("~/Views/CategoriasDeProdutos/_Detalhes.cshtml", produto);
RenderPartial
returns void
and writes directly to the request output.
@Html.Partial("~/Views/CategoriasDeProdutos/_Detalhes.cshtml", produto);
Partial
returns a String
with the Partial HTML. Can be assigned to a variable.
There is a complete article in CodeProject that explains this in more details.