Class attribute with predetermined values

Asked

Viewed 412 times

3

I want to create a class attribute for sex (M or F), that by default one can already select. It is possible to create an array with these values (M and F) already default without having to type, or should do so in the same layout?

[DisplayName("SEXO")]
[StringLength(1, MinimumLength = 1, ErrorMessage = "Selecione o Sexo")]
public string Sexo { get; set; }

The way it is here (above) would be a field for the person to type, I thought to do like a:

public list<Sexo>...

AMENDING

namespace Projeto.Models
{
    public enum Sexo { M = 1, F = 2 }

    public class Pessoa
    {
        [Key]
        public int PessoaID { get; set; }

        [DisplayName("Nome")]
        [Required(ErrorMessage = "Preencha o nome")]
        [StringLength(255, MinimumLength = 3, ErrorMessage = "O nome deve ter de 3 a 255 caracteres")]
        public string Nome { get; set; }

        ...

        [DisplayName("SEXO")]
        [Required(ErrorMessage="Selecione o sexo")]
        [StringLength(1, MinimumLength = 1, ErrorMessage = "Selecione o Sexo")]
        public Sexo Sexo { get; set; }

    }
}

in making Add-Migration I receive the following message:

inserir a descrição da imagem aqui

  • If you can use a Enum in the case!

  • thus? public enum Sexo { M, F } ? but it wouldn’t have to have the {Get; Set;} ??

  • I put in a reply!

  • It worked for you @Fabiosouza ???

  • 1

    So Virgil, I did, but I haven’t tested the functionality yet, so test and work, I mark as accepted. Thank you for so far

  • Remove that Stringlenght and the problem

Show 1 more comment

1 answer

2


  • when giving Add-Migration I receive this message: The property 'Sexo' is not a String or Byte array. Length can only be configured for String and Byte array properties., I will add to the question what I have done

  • @Fabiosouza remove Stringlenght has no need. Remember that it records whole !!!

  • Really apart from this did not appear the message, but appeared a new message: Data truncated for column 'Sexo' at row 1, The public enum Sexo { M = 1, F = 2 } have to be in separate classes? then I put in the same class Pessoa

  • @Fabiosouza if before had data of these messages, has data in the table???

  • I don’t know what you mean

  • Do you have data in this table? @Fabiosouza?????????????

  • 1

    I really had data on the table, but since I was null I thought it would not make a difference, but now it worked out well. Thanks

Show 2 more comments

Browser other questions tagged

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