Saltar apartados

Validation model

Already we have worked with the presentation of the data, and now is the moment to validate that the data that enter are correct, for afterwards store it (this will be in the next session).

To validate the data will have to press the button store and ¿to where carries us the surroundings?. Pues To any place because we have to create a new method that gestione the information entered. This method has to call equal that the one of presentation but with a series of difference.

  1. We Will have to happen like parameter the model with which have worked in the edition, in our case catalogador
  2. It has to include, with a definition, that the data receive by POST. This achieves putting [HttpPost] before the statement of the method

Therefore the result would be the following

Accion Editar con método POST

Control in an exception all the logic of storage. First we validate, in correct case will store the data and in contrary case showed again the sight.

If we tested it will see that it works and that redirects us to the method Index, that if we have defined properly (including the sight) will see that it carries us there, because the model has been validated.

The question that do us now is, ¿what validates?. We have not defined any rule that have to fulfil our model.

Rules of validation

To assign rules, have to acudir again to the model and begin to add definitions. Stop can add them have to include the spaces of names

using System.ComponentModel.DataAnnotations;

using System.ComponentModel;

using System.Web.Mvc;

So that we can use the attributes of validation.

We go to put a very basic, that was compulsory the field CSS [Required].

Definir un campo obligatorio

Also in occasions does not like us that it appear as it labels the name of the field of the model, if no that we want an own description in the case of Css want to that it was Leaf of styles [DisplayName].

Definimos la descripción del campo

If we generated the solution and edited, will see that already remains reflected the new description

Visualizar editor

Besides if we do not put any value in the field Leaf of style and pressed the button Update, will observe that it appears us a message of error.

Validación de la acción editar

Other definitions that can find are: (http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-2-modelmetadata.html)

  • [HiddenInput] Indicates that the field is not visible
  • [UIHint("TemplateControl")] Indicates the staff that has to use to show this field. There Is available an example of staff for the data of the model.
  • [ScaffoldColumn(false)] In the cases in which the type of field do not know like representing it, say him that it ignore it
  • DisplayColumn Define the behaviour of Html.DisplayText() And Html.DisplayTextFor().
  • DataType Can specify the type of data that is. Of this way can personalizar the format. For example if you specify as [DateType(DateType.It dates)] the format that applies him is {0:d}
  • DisplayFormat [DisplayFormat(DataFormatString = "{0:c}")]
  • [ReadOnly(true)] Field of only reading
  • [Range(EntreValor, HastaValor, ErrorMessage="The value has to be between EntreValor and HastaValor")] Indicated the valid rank for a number
  • [StringLength(12)] maximum Size that can occupy the chain of text
  • [RegularExpression("$ul>

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