我已成功验证了我的MVC4项目中实体级别的大多数字段。但是,有两个字段名为" IsWorking"和" WorkingCity"我有一个问题来验证它们。情况如下:
1)当用户选择IsWorking下拉列表为"是" " WorkingCity"显示下拉列表并对此字段进行验证。
2)当用户选择IsWorking下拉列表为"否" " WorkingCity"隐藏下拉列表,此字段应忽略验证。但是,我无法在实体级别上提供此功能。如何在实体层面上提供它?如果在实体级别上有解决方案,请不要使用jQuery.validate()方法在View级别上建议解决方案?
我已经按照https://foolproof.codeplex.com/和RequiredIf Conditional Validation Attribute上的stackoverflow等类似文章的所有步骤进行了操作,但它没有解决问题。有没有想过在实体层面解决这个问题?提前谢谢。
实体:
[Display(Name = "Do you work?")]
[Required(ErrorMessage = "Required field")]
public int IsWorking { get; set; }
[Display(Name = "City")]
[Required(ErrorMessage = "Required field")]
public string WorkingCity { get; set; }
查看:
@Html.LabelFor(m=>m.Applicant.IsWorking)
@Html.DropDownListFor(m => m.Applicant.IsWorking), new { id = "isWorking", onchange="showHideWorking()"})
@Html.ValidationMessageFor(m => m.Applicant.IsWorking, null , new { @class = "ValidationErrors" })
@Html.LabelFor(m=>m.Applicant.WorkingCity)
@Html.DropDownListFor(m => m.Applicant.WorkingCity, new { id = "workingCity"})