I have managed to validate most of the fields on entity level in my MVC4 project. However, there are 2 fields named "IsWorking" and "WorkingCity" and I have a problem to validate them. The situations as the following:
1) When user select IsWorking dropdownlist as "Yes" the "WorkingCity" dropdownlist is shown and validation is done for this field.
2) When user select IsWorking dropdownlist as "No" the "WorkingCity" dropdownlist is hidden and validation should be ignored for this field. However, I cannot provide this on Entity level. How can provide it on entity level? Please do not suggest a solution on View level by using jQuery.validate() method if there is a solution on entity level?
I have followed all the steps on similar articles in stackoverflow and etc. as on https://foolproof.codeplex.com/ and RequiredIf Conditional Validation Attribute but it did not solved the problem. Any idea to solve this on entity level? Thanks in advance.
Entity:
[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; }
View:
@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"})