attribute dependent on another field

后端 未结 5 1558
抹茶落季
抹茶落季 2020-11-27 04:03

In a model of my ASP.NET MVC application I would like validate a textbox as required only if a specific checkbox is checked.

Something like

public bo         


        
5条回答
  •  执念已碎
    2020-11-27 04:52

    Using NuGet Package Manager I intstalled this: https://github.com/jwaliszko/ExpressiveAnnotations

    And this is my Model:

    using ExpressiveAnnotations.Attributes;
    
    public bool HasReferenceToNotIncludedFile { get; set; }
    
    [RequiredIf("HasReferenceToNotIncludedFile == true", ErrorMessage = "RelevantAuditOpinionNumbers are required.")]
    public string RelevantAuditOpinionNumbers { get; set; }
    

    I guarantee you this will work!

提交回复
热议问题