Saltar apartados

Custom templates

As we have commented in the introduction, any models that go to be edited or visualised, can be personalizada his presentation by means of the staff.

Folders staff

ASP.NET MVC Has a structure of folders very clear-cut to the hour to look for where have defined the staff, and besides, separates it by which are for edition and those that are for visualisation.

Visualisation
  • /Views/ControllerName/DisplayTemplates/TemplateName.aspx Or /Views/ControllerName/DisplayTemplates/TemplateName.ascx
  • /Views/Shared/DisplayTemplates/TemplateName.aspx Or /Views/Shared/DisplayTemplates/TemplateName.ascx

And in the case that we use areas (does not go to explain initially in this course)

  • /Areas/AreaName/Views/ControllerName/DisplayTemplates/TemplateName.aspx Or /Areas/AreaName/Views/ControllerName/DisplayTemplates/TemplateName.ascx
  • /Areas/AreaName/Views/Shared/DisplayTemplates/TemplateName.aspx Or /Areas/AreaName/Views/Shared/DisplayTemplates/TemplateName.ascx
Edition
  • /Views/ControllerName/EditorTemplates/TemplateName.aspx Or /Views/ControllerName/EditorTemplates/TemplateName.ascx
  • /Views/Shared/EditorTemplates/TemplateName.aspx Or /Views/Shared/EditorTemplates/TemplateName.ascx

And in the case that we use areas (does not go to explain initially in this course)

  • /Areas/AreaName/Views/ControllerName/EditorTemplates/TemplateName.aspx Or /Areas/AreaName/Views/ControllerName/EditorTemplates/TemplateName.ascx
  • /Areas/AreaName/Views/Shared/EditorTemplates/TemplateName.aspx Or /Areas/AreaName/Views/Shared/EditorTemplates/TemplateName.ascx

My recommendation is to use the extension ascx to differentiate clearly that they are staff, of what are seen for example.

When we created an empty project, will see that they do not exist these folders (DisplayTemplates or EditorTemplates) in the structure of directories. In this case will have to create it we same.

Creating a staff

There Is a good number of staff predefinidas. Can consult in the following page ASP.NET MVC 2 Templates, Part 3: Default Templates, and is possible to modify the behaviour of any of them simply going to the corresponding folder (creating the subcarpetas that do fault) and editing the file in question.

A very simple example that use in this page, that serves to explain facilmente the use of the staff, is the one to replace the visualisation of the chains of text.

Following the criterion that have commented before, have to access to /Views/Shared/DisplayTemplates/String.ascx

Plantilla para las cadenas de texto

Knowing that the code that corresponds him to String is

Código ASP.NET de la plantilla string

We modify it adding a chain of text (Hello there!)

Código para la cadena de texto incluyendo un breve mensaje

The result will be the following every time that it represent a chain of text, the value that correspond him (Model) + the chain of text that have added him.

Resultado de modificar la plantilla de cadena de texto

The example is very basic, but is very useful to understand that any element can be personaliza his appearance from the staff.

Interesting links

Computing service ASP.NET MVC 2 Framework


Universidad de Alicante
Carretera de San Vicente del Raspeig s/n
03690 San Vicente del Raspeig
Alicante (Spain)

Tel: (+34) 96 590 3400

Fax: (+34) 96 590 3464

For further information: informacio@ua.es, and for enquiries on this web server: webmaster@ua.es

Carretera San Vicente del Raspeig s/n - 03690 San Vicente del Raspeig - Alicante - Tel. 96 590 3400 - Fax 96 590 3464