1 回答

TA貢獻1828條經驗 獲得超3個贊
這應該會給你你正在尋找的東西。
public class Report : ValidationAttribute
{
public int Score { get; set; }
public string Comment { get; set; }
public int[] ReasonIds { get; set; }
protected override ValidationResult IsValid(
object value, ValidationContext validationContext)
{
if(Score < 4 && (string.IsNullOrEmpty(Comment) || ReasonIds.Count() < 1))
{
return new ValidationResult(GeScoreErrorMessage());
}
return ValidationResult.Success;
}
private string GeScoreErrorMessage()
{
return $"If Score < 4 Comment and Reasons must be provided";
}
}
- 1 回答
- 0 關注
- 109 瀏覽
添加回答
舉報