简体   繁体   English

如何验证模型取决于使用ASP.NET Core Web API的布尔数据类型值

[英]How to validate model depends on bool datatype value using asp.net core web api

I have created a model using asp.net core web API and I have to validate the property depends on bool data type property value. 我已经使用asp.net核心Web API创建了一个模型,并且必须验证该属性取决于bool数据类型属性值。

If the IsWaitingList property is true , the WaitingListEncounterOrOtherEncounter property is required property, else it's not a mandatory field. 如果IsWaitingList属性为true,则WaitingListEncounterOrOtherEncounter属性为必填属性,否则不是必填字段。

public class CenterConfiguration
    {
        public Guid Id { get; set; } = Guid.NewGuid();
        public bool? IsWaitingList { get; set; }
        [Required(ErrorMessage = "Please enter waiting list encounter or other encounter")]
        public int WaitingListEncounterOrOtherEncounter { get; set; }
    } 

If anybody knows, please let me know. 如果有人知道,请告诉我。

You can implement IValidatableObject to check complex validation conditions. 您可以实现IValidatableObject来检查复杂的验证条件。

public class CenterConfiguration : IValidatableObject {
    public Guid Id { get; set; } = Guid.NewGuid();
    public bool? IsWaitingList { get; set; }
    public int? WaitingListEncounterOrOtherEncounter { get; set; }

    public IEnumerable<ValidationResult> Validate(ValidationContext validationContext) {
         if (IsWaitingList && !WaitingListEncounterOrOtherEncounter.HasValue) {
             yield return new ValidationResult(
                 "Please enter waiting list encounter or other encounter",
                 new[] { "WaitingListEncounterOrOtherEncounter" }
             );
         }
    }
}

Also note that I changed WaitingListEncounterOrOtherEncounter from int to int? 另请注意,我将WaitingListEncounterOrOtherEncounterint更改为int? so you can actually leave this value blank. 因此您实际上可以将此值留空。

See also How do I use IValidatableObject? 另请参阅如何使用IValidatableObject?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP.NET 内核 Web API - 如何使用数据注释根据指定值进行验证 - ASP.NET Core Web API - How to validate based on specified value using data annotation 如何在ASP.NET Core 2 Web API中仅验证模型状态的一部分 - How to validate only a part of Model State in asp.net core 2 web api 如何有条件地验证 ASP.NET 核心 Web ZDB974238714CA8DE634A7CE1D08 中的整个 model? - How to conditionally validate entire model in ASP.NET Core Web API? 如何通过仅允许在 asp.net 核心 web Z8A35DA52ED1057271 的 Model 中分配的属性来验证请求正文 - How to Validate Request Body by allowing only attributes assigned in the Model of an asp.net core web api app 根据Model Asp.net Core API验证Json String - Validate Json String against Model asp.net core api ASP.NET 内核 Web API - 如何在 EF 中验证 EndDate 大于 StartDate - ASP.NET Core Web API - How to validate EndDate greater than StartDate in EF 如何验证 asp.net 核心 3.0 web api 的获取请求中的参数? - How to Validate parameter in asp.net core 3.0 web api's get request? 如何验证 ASP.NET Core Web API 中必须有 2 个字段之一? - How to validate must have 1 of 2 fields in ASP.NET Core Web API? ASP.NET Core Web API 模型绑定行为更改 - ASP.NET Core Web API Model binding behavior change asp.net core web api 2中的抽象类模型绑定 - Abstract class model binding in asp.net core web api 2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM