我正在为一家保险公司开发ASP.NET MVC 5 Web应用程序。
从一开始,我就使用Entity Framework帮助创建了视图和控制器,CRUD做得很好。
但是当我不得不使用更复杂的模型时,我使用剃刀来处理基于前端的表单和模型的属性。从技术上讲,我正在研究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
}
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; }
BeneficiareEnfantGarantieMaldie
[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; }
但是当我在public enum SexeEnum
和Genre
类的BeneficiareEnfantGarantieMaladie
属性中使用BeneficiareConjointGarantieMaladie
时,我没有向其中添加[Required]
注释这些字段,但是当我尝试在应用程序中填写表单时,出现错误消息,该字段为必填字段。
可能是什么问题?
我希望填写表格并将这些字段留空,但实际输出是错误消息。
答案 0 :(得分:0)
您说:“我正在BeneficiareEnfantGarantieMaladie和BeneficiareConjointGarantieMaladie类的Genre属性中使用公共枚举SexeEnum,但没有在这些字段中添加[Required]注释”,但是在您发布的代码中有一个[Required] ]属性在两个模型中的作用。
我建议使该属性可为空:
public SexeEnum? Genre { get; set; }