Saltar apartados

Helpers

The helpers are a very powerful tool to generate our own code HTML inside the sights. This simplifies enormously the code that generate in the sight because we avoided to have to generate a lot of code in each call to the sights.

In a lot of occasions no only will use it to generate HTML but to work with chains of text. For example can use to truncate chains of text (in the tutorial of the shop of music explains it with detail). We have to remember that the sight has to be very readable by what have to avoid define us functions inside this. Some authors think that it is better that all are in the own sight because like this is more comfortable of gestionar (put it at the end of the sight). Our experience indicates us the contrary, better simplify the sight and leave the complexity of the logic to other classes, that even allows us can reaprovecharlo in several sights.

We go to define two forms to work with Helpers. In both cases the first is to create the folder Helpers in case that it did not exist (right button on the project, Add, New folder).

Crear carpeta para crear helpers

In said folder created the classes (right button, Add, Class …) for the generation of a label (label) with the option to be able to say if the field is compulsory or no

For this we created a class LabelHelperUA.cs With the following code.

Modelo de clase

Than in summary what does is to receive the identifier of the field to the that does reference, the description of the label and if it is compulsory or no. It prepares the code with each one of the variable and with the option of * in the case that it indicate that the field is compulsory. The result gives it back like text that will be able to be showed by the screen.

The form to call it from the sight is very simple. We put the name of the class with the concrete method that want to call. With his parameters.

Llamada de helper desde la vista

But to be able to find the class have to include a reference to the folder where find the helpers. The general will be in “Project”.Helpers. We Will use the directive Import to include the(s) class(s).

Referencias para usar los helpers

In the previous case have created an independent class to Html that is the class by defect that called for the basic operations in the sights.

Can extend the methods of the class Html to include our own functions. A very simple example can be to generate the code of our menus.

In this case we created us a class to the equal that before but that has to be of type static (so much the class like the methods) and have like name LabelExtensions. Of this way will include the methods that define to the class Html of a transparent form.

In the following example do use of StringBuilder to accelerate the work with chains of text and at the end, and to the equal that before , give back a chain of text with the code HTML resultant. Inside the class do use of other methods of the class Html, since we have of a parameter of type HtmlHelper, that allows it to us.

Ejemplo de helper html

A more complex example of use of Helpers is the possibility to define a label (label) and in the first case, but without having to define if it is compulsory or no. This information will obtain it of the definition of the model (look validation of models).

The location of the methods will do of equal way that before (inside the class LabelExtensions), but unlike before go to give back MvcHtmlString instead of a simple chain of testo. The parameters are something different and does a call to another method with the information of the validation of the model.

Of this way will be able to consult information like the name, if it is required, etc.,

We generate all the labels with TagBuilder (are not giving back a chain of text, this have to have it clear) and go assigning the attributes to the label HTML and the description of the label.

Ejemplo de un helper avanzado

It can seem more complicated, but when being so modular is very simple to comprise and especially to expand.

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