如何为通用列表属性上的必需属性添加不显眼的验证?

时间:2015-10-01 16:00:45

标签: c# jquery asp.net-mvc generics unobtrusive-validation

我使用以下堆栈溢出答案中的解决方案在列表属性上添加Required属性:

Required Attribute on Generic List Property

[AttributeUsage(AttributeTargets.Property)]
public sealed class CannotBeEmptyAttribute : ValidationAttribute
{
    private const string defaultError = "'{0}' must have at least one element.";
    public CannotBeEmptyAttribute ( ) : base(defaultError) //
    { 
    }

    public override bool IsValid ( object value )
    {
      IList list = value as IList;
      return ( list != null && list.Count > 0 );
    }

    public override string FormatErrorMessage ( string name )
    {
        return String.Format(this.ErrorMessageString, name);
    }
}

如何将此转换为jquery.validate.unobtrusive使用的客户端验证?

0 个答案:

没有答案