Best Way to Implement Dataannotations

Asked

Viewed 55 times

1

There is some difference in the following implementations?

 [Required, StringLength(150, ErrorMessage = "Insira no máximo 150 caracteres")]
 [Index(IsUnique =true)]
 public int MyProperty { get; set; }

 [Required]
 [StringLength(150, ErrorMessage ="Insira no máximo 150 caracteres")]
 [Index(IsUnique =true)]
 public int MyProperty { get; set; }

I ask to know, if there are moments that are used in the first way and in others in the second. Or is the same thing ?

  • You mean, in separate lines or together?

  • @mustache, that. If I wear the Required on the same line as the StringLength or in different lines, as I always see around.

2 answers

3

If you just leave it like this:

[Required, StringLength(150, ErrorMessage = "Insira no máximo 150 caracteres")]

The custom message will shoot only for the string with a maximum size of 150 characters. And so will be the same thing

[Required]
[StringLength(150, ErrorMessage ="Insira no máximo 150 caracteres")]

If you are using jquery validate a standard message will be sent to Required, but if you want to leave the custom message:

[Required(ErrorMessage = "O campo xxx deverá ser preenchido!)]

And I think the best way is to use it as in the example:

public class Produto
{
    [Key]
    [Column(Order = 0)]
    public int ID { get; set; }

    [Required(ErrorMessage = "Digite o nome do produto."), Column(Order = 1)]
    [MinLength(5, ErrorMessage = "O tamanho mínimo do nome são 5 caracteres.")]
    [StringLength(200, ErrorMessage = "O tamanho máximo são 200 caracteres.")]
    public string Nome { get; set; }

    [Display(Name="Quantidade")]
    [DisplayFormat(DataFormatString = "{0:n2}",
            ApplyFormatInEditMode = true,
            NullDisplayText = "Estoque vazio")]
    [Range(10, 25, ErrorMessage = "A Qtde deverá ser entre 10 e 25.")]
    [Column(Order = 2)]
    public double Qtde { get; set; }

    [DisplayFormat(DataFormatString = "{0:n2}",
            ApplyFormatInEditMode = true,
            NullDisplayText = "Sem preço")]
    [Range(3, 500, ErrorMessage = "O preço deverá ser entre 3 e 500.")]
    [Column(Order = 3)]
    public decimal Preco { get; set; }

    [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}")]
    [Column(Order = 4)]
    [Display(Name="Vencimento")]
    public DateTime Dt_Vencto { get; set; }
}

3


For the record, the answer is valid for all types of Attributes and not only DataAnnotations.

There is no functional difference between the two statements.

It’s more a matter of liking "style" programming.

Most of the time, me I prefer to use the Attributes in the same line, unless they have very big names or I end up using many parameters, then I leave in separate lines to facilitate reading.

So:

[PrimeiroAtributo, SegundoAtributo]
public string Nome { get; set; }

It’s exactly the same thing

[PrimeiroAtributo]
[SegundoAtributo]
public string Nome { get; set; }
  • when @Angélicaflausino says: ;"[Required, StringLength(150, ErrorMessage = "Insira no máximo 150 caracteres")] The custom message will shoot only for the string with a maximum length of 150 characters. And so it will be the same thing" Attribute StringLength will work, this information does not proceed then ?

  • I can’t talk about what she meant. But I can tell you for sure that my answer is right. I think she meant the same thing I did, only she ended up getting a little tangled and talking too much, so I decided to post my answer, to have something more direct and succinct.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.