多个输入模型应该有任何一个必需的字段验证,下面提到了一个示例,请帮助解决它。下面我有三个输入字段,在这三个输入字段中应该是必需的。
public class search
{
public string Sample1 { get; set; }
public string Sample2 { get; set; }
public string Sample3 { get; set; }
}
答案 0 :(得分:2)
使用IValidatableObject
public class search : IValidatableObject
{
public string Sample1 { get; set; }
public string Sample2 { get; set; }
public string Sample3 { get; set; }
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
var results = new List<ValidationResult>();
if (string.IsNullOrEmpty(Sample1) && string.IsNullOrEmpty(Sample2) && string.IsNullOrEmpty(Sample3))
{
results.Add(new ValidationResult("One of them is required."));
}
return results;
}
}
答案 1 :(得分:0)
returning