Organization and maintenance of Bootstrap

4

I'm starting with the use of Bootstrap with MVC 4 and during the planning I noticed that the main screen of the system will have many divs. Many of them can be repeated (exactly the same) in other screens.

To facilitate future maintenance and organization of the code I was thinking of separating some divs into html files, ie my main screen would have about 4 includes of other html, which generate 4 additional requisitions per visit. I do not know if it's worrying.

Does anyone have any tips for improving code organization and future maintenance?

    
asked by anonymous 13.08.2014 / 22:12

1 answer

3

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.

    
13.08.2014 / 22:18