所需属性取决于另一个属性的值(ASP.NET Core Web Api)

问题描述 投票:0回答:1

我有一个模型类,并且仅当另一个参数的值是某些东西时,我才想对参数“ required”进行设置。

[JsonConverter(typeof(JsonStringEnumConverter))]
[Required]
public AddressTagEnum AddressTagId { get; set; }

[RequiredIf("AddressTagId", 3)]
[MaxLength(20)]
public string AddressTagOther { get; set; }

我该如何实现?预先感谢。

c# asp.net-core asp.net-web-api data-annotations
1个回答
0
投票

有几种方法可以解决您的问题:

  1. 没有标准方法可以做到这一点
  2. 存在诽谤者,例如Expressive Annotations应该有帮助
  3. 有一个内置属性Remote,可让您执行服务器验证,例如:[Remote(action: "VerifyEmail", controller: "Users")]请参见docs
© www.soinside.com 2019 - 2024. All rights reserved.