此字段是必需的

This field is required

我正在为一家保险公司开发 ASP.NET MVC 5 Web 应用程序。

一开始,我使用 Entity Framework 创建视图和控制器的帮助,CRUD 做得很好。

但是当我不得不使用更复杂的模型时,我使用 razor 来处理基于前端表单和模型的属性。从技术上讲,我正在研究 6 个六模型 类。

我正在使用的模型 类 具有一些必需的属性。

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
    }

受益人EnCasDeces

    [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; }

BeneficareEnfantGarantieMaladie

    [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; }

但是当我在 Genre 属性 中为 BeneficiareEnfantGarantieMaladieBeneficiareConjointGarantieMaladie 类 使用 public enum SexeEnum 时,我没有t 将 [Required] 注释添加到这些字段,但是当我尝试在应用程序中填写表单时,我收到一个错误,指出该字段是必需的。

可能是什么问题?

我希望填写表格并将这些字段留空,但实际输出是一条错误消息。

您说:"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" 但在您发布的代码中,两个模型中的 属性 上都有一个 [Required] 属性。

我建议使 属性 可为空:

public SexeEnum? Genre { get; set; }