仅当上述条件为真时,我才尝试制作标签 [Required]

I'm trying to make a label [Required] only if the condition above is true

我正在尝试制作一个注册页面,如果用户正在积极工作,我想要求工作详细信息(公司名称、职位等)部分。但是找不到解决方案

我会说你在你的模型中实现了一个看起来有点像这样的验证器。

public class YourModelClassName : IValidatableObject
{

  // Loads of properties Name, Phone yada yada...

  public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
  {
    if (EmployeeActive == true 
        && String.IsNullOrEmpty(CompanyName)
        && String.IsNullOrEmpty(Position)
        // add more if needed
       )
    {
      yield return new ValidationResult(
        // some message here.
        $"When you are an active employee you must fill in the basic data for your employment...",
        new[] { nameof(EmployeeActive) });
     }
  }
}

在此处阅读更多内容:https://docs.microsoft.com/en-us/aspnet/core/mvc/models/validation?view=aspnetcore-6.0#ivalidatableobject