Search code examples
sql-serverrazorasp.net-mvc-5entity-framework-6data-annotations

This field is required


I'm working on an ASP.NET MVC 5 web application for an insurance company.

In the very beginning, I used the assistance to create the views and controllers using Entity Framework, the CRUD was doing just fine.

But when I had to use more complex models, I used razor to handle the front end forms based and the properties of the models. Technically there are 6 six model classes that I'm working on.

The model classes I'm using have some required properties.

BulletinAdhesionContentRMA

    [Required]
    [Display(Name = "Type de bulletin")]
    public TypeBulletinEnum TypeBulletin { get; set; }

    [Required]
    [StringLength(50)]
    [Display(Name = "N° d'adhésion")]
    public string NumeroAdhesion { get; set; }

    [Required]
    public Employeur Employeur { get; set; }

    [Required]
    [StringLength(15)]
    public string Nom { get; set; }

    [Required]
    [StringLength(15)]
    [Display(Name = "Prénom")]
    public string Prenom { get; set; }

    [StringLength(15)]
    [Display(Name = "Nom de jeune fille (Si affilié de sexe féminin)")]
    public string NomJeuneFille { get; set; }

    [Required]
    public Sexe Genre { get; set; }

    [Required]
    [Display(Name = "Situation de famille")]
    public SituationFamille SituationFamiliale { get; set; }

    [Required]
    [DataType(DataType.Date)]
    [Display(Name = "Date de naissance")]
    public DateTime DateNaissance { get; set; }

    [Required]
    [Display(Name = "Type de document d'identification (Cin, passeport, etc...)")]
    public TypeDocumentIdentification TypeDocument { get; set; }

    [Required]
    [StringLength(15)]
    [Display(Name = "N° document")]
    public string NumeroDocument { get; set; }

    [Required]
    [StringLength(60)]
    public string Adresse { get; set; }

    [Required]
    [StringLength(20)]
    public string Ville { get; set; }

    [Required]
    [StringLength(10)]
    public string Pays { get; set; }

    [StringLength(15)]
    [Phone]
    [Display(Name = "Téléphone")]
    public string Telephone { get; set; }

    [Required]
    [DataType(DataType.Date)]
    [Display(Name = "Date d'affiliation")]
    public DateTime DateAffiliation { get; set; }

    [Required]
    [DataType(DataType.Date)]
    [Display(Name = "Date d'entrée en fonction")]
    public DateTime DateEntreeFonction { get; set; }

    [Display(Name = "Catégorie du personnel")]
    [StringLength(50)]
    public string CategoriePersonnel { get; set; }

    [StringLength(50)]
    public string Emploi { get; set; }

    [StringLength(50)]
    public string Matricule { get; set; }

    [DataType(DataType.Currency)]
    [Display(Name = "Salaire annuel (ou capital)")]
    public double SalaireAnnuel { get; set; }

    public BeneficiareConjointGarantieMaladie[] BeneficiareConjoints { get; set; }
    public BeneficiareEnfantGarantieMaladie[] BeneficiareEnfants { get; set; }
    public BeneficiareEnCasDeces[] BeneficiareEnCas { get; set; }

    [Required]
    [DataType(DataType.Date)]
    [Display(Name = "Le :")]
    public DateTime FaitLe { get; set; }

    [Required]
    [StringLength(60)]
    [Display(Name = "Fait à")]
    public string FaitA { get; set; }

    public enum Sexe
    {
        Homme = 0,
        Femme = 1
    }

    public enum SituationFamille
    {
        [Display(Name = "Célibataire")]
        Celibataire = 0,
        [Display(Name = "Marié (e)")]
        Mariee = 1,
        [Display(Name = "Divorcé (e)")]
        Divorcee = 2,
        [Display(Name = "Veuf (ve)")]
        Veufs = 3,
        [Display(Name = "Décédé (e)")]
        Decede = 4
    }

    public enum TypeDocumentIdentification
    {
        Cin = 0,
        Passeport = 1,
        Permis = 2
    }

    public enum TypeBulletinEnum
    {
        [Display(Name = "Nouvelle Adhesion")]
        NouvelleAdhesion,
        [Display(Name = "Rectificatif")]
        Rectificatif
    }

BeneficiaireEnCasDeces

    [Key]
    public string Code { get; set; }

    [Required]
    [StringLength(50)]
    public string Nom { get; set; }

    [Required]
    [StringLength(50)]
    [Display(Name = "Prénom")]
    public string Prenom { get; set; }

    [Required]
    [DataType(DataType.Date)]
    [Display(Name = "date de naissance")]
    public DateTime DateNaissance { get; set; }

    [StringLength(50)]
    public string LienDeParente { get; set; }

BeneficiareEnfantGarantieMaladie

    [Key]
    public string Code { get; set; }

    [StringLength(50)]
    public string Nom { get; set; }

    [StringLength(50)]
    [Display(Name = "Prénom")]
    public string Prenom { get; set; }

    [Required(AllowEmptyStrings = true)]
    public SexeEnum Genre { get; set; }

    [DataType(DataType.Date)]
    [Display(Name = "Date de naissance")]
    public DateTime DateNaissance { get; set; }

BeneficiareConjointGarantieMaladie

    [Key]
    public string Code { get; set; }

    [StringLength(15)]
    public string Nom { get; set; }

    [StringLength(15)]
    [Display(Name = "Prénom")]
    public string Prenom { get; set; }

    [Required(AllowEmptyStrings = true)]
    public SexeEnum Genre { get; set; }

    [DataType(DataType.Date)]
    [Display(Name = "Date de naissance")]
    public DateTime DateNaissance { get; set; }

But when I'm using the public enum SexeEnum in the Genre property for BeneficiareEnfantGarantieMaladie and BeneficiareConjointGarantieMaladie classes, I didn't add the [Required] annotation to those fields, but when I try to fill the form in the application, I get an error that the field is required.

What could the problem be?

I expect to fill the form and leave those fields empty, but the actual output is an error message.


Solution

  • You say: "I'm using the public enum SexeEnum in the Genre property for BeneficiareEnfantGarantieMaladie and BeneficiareConjointGarantieMaladie classes, I didn't add the [Required] annotation to those fields" but in the code you posted there is a [Required] attribute over the property in both models.

    I suggest making the property nullable:

    public SexeEnum? Genre { get; set; }