1

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"})


Community
  • 1
  • 1
Jack
  • 1
  • 21
  • 118
  • 236
  • 1
    You need some kind RequiredIf validation attribute. Unfortunately Asp.Net MVC does not have such attribute. However You can use third party library for this [MVC Foolproof Validation](https://foolproof.codeplex.com) – YuriyP Jan 10 '15 at 20:52
  • Thanks a lot for reply. Actually RequiredIf validation attribute seems to be good enough and I installed it. However, there is an error when trying to use it in MVC4. Any idea to solve the problem? Could not load file or assembly 'MVC.Foolproof.Validation' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040) – Jack Jan 10 '15 at 22:03
  • 1
    You can also just write a `RequiredIf`. It isn't complicated. Here's one... http://stackoverflow.com/a/15975880/1950321 – jamesSampica Jan 11 '15 at 02:12
  • @Shoe @YuriyP Many thanks for your help. I have tried to use it, but I do not know the value of "ErrorMessageResourceType" as below: ` [RequiredIf("IsWorking", 31, ErrorMessageResourceName="RequiredField", ErrorMessage = "Required field", ErrorMessageResourceType = typeof(?????))] public string WorkingCity { get; set; } ` – Jack Jan 11 '15 at 09:34
  • 1
    You dont need `ErrorMessageResourceName` or `ErrorMessageResourceType` – jamesSampica Jan 11 '15 at 16:44
  • @Shoe: In MVC4, I have not managed to work properly on client side and it fires up the validation no matter what the DesiredValue is. Any help pls? – Jack Jan 11 '15 at 18:56
  • 1
    Anyway, finally I have managed to work it on both; client & server side with the help of @JaroslawWaliszko's wonderful example on https://github.com/JaroslawWaliszko/ExpressiveAnnotations. Thanks a lot Shoe and YuriyP to you also. – Jack Jan 13 '15 at 22:26

0 Answers0