Asp.net core 如何在asp.net core 2.1或更高版本中创建所需的自定义验证程序

Asp.net core 如何在asp.net core 2.1或更高版本中创建所需的自定义验证程序,asp.net-core,data-annotations,customvalidator,Asp.net Core,Data Annotations,Customvalidator,如何在asp.net core中使用客户端和服务器端验证创建自定义模型验证。我的要求是检查属性的值,如果它满足该值,则应启用required字段。其他明智的做法是禁用。注释也可以应用于对象属性 public class TradeModel { public bool TradingObjectives { get; set; } [RequiredIf("TradingObjectives","true",ErrorMessage="required")] pu

如何在asp.net core中使用客户端和服务器端验证创建自定义模型验证。我的要求是检查属性的值,如果它满足该值,则应启用required字段。其他明智的做法是禁用。注释也可以应用于对象属性

public class TradeModel
{   
    public bool TradingObjectives { get; set; }

    [RequiredIf("TradingObjectives","true",ErrorMessage="required")]
    public int Hedge{ get; set; }

    [RequiredIf("TradingObjectives","true",ErrorMessage="required")]
    public AddressModel Address{ get; set; }

}

public class AddressModel 
{
    public long AddressId { get; set; }

    [Required]
    public string Address1 { get; set; }

    public string Address2 { get; set; }
}
我认为您应该使用自定义验证器。您还可以使用为Fluent验证添加客户端支持

您可以编写自定义验证器,甚至数据库查询。这对您来说是一个了不起的解决方案。

请尝试参考