您可以創建一個CustomValidationAttribute
以此爲考試PLE:
public class GreaterThanAttribute : ValidationAttribute, IClientValidatable
{
private readonly string _testedPropertyName;
private readonly bool _allowEqualValues;
private readonly string _testedPropertyDisplayName;
public override string FormatErrorMessage(string displayName)
{
return string.Format(ErrorMessages.GreaterThan_Message, displayName, _testedPropertyDisplayName);
}
public GreaterThanAttribute(string testedPropertyName, Type resourceType, string testedPropertyDisplayNameKey, bool allowEqualValues = false)
{
_testedPropertyName = testedPropertyName;
_allowEqualValues = allowEqualValues;
var rm = new ResourceManager(resourceType);
_testedPropertyDisplayName = rm.GetString(testedPropertyDisplayNameKey);
}
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
var propertyTestedInfo = validationContext.ObjectType.GetProperty(_testedPropertyName);
if (propertyTestedInfo == null)
{
return new ValidationResult(string.Format("unknown property {0}", _testedPropertyName));
}
var propertyTestedValue = propertyTestedInfo.GetValue(validationContext.ObjectInstance, null);
if (value == null || !(value is Decimal))
{
return ValidationResult.Success;
}
if (propertyTestedValue == null || !(propertyTestedValue is Decimal))
{
return ValidationResult.Success;
}
// Compare values
if ((Decimal)value >= (Decimal)propertyTestedValue)
{
if (_allowEqualValues && value == propertyTestedValue)
{
return ValidationResult.Success;
}
else if ((Decimal)value > (Decimal)propertyTestedValue)
{
return ValidationResult.Success;
}
}
return new ValidationResult(FormatErrorMessage(validationContext.DisplayName));
}
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
var rule = new ModelClientValidationRule
{
ErrorMessage = FormatErrorMessage(metadata.DisplayName),
ValidationType = "greaterthan"
};
rule.ValidationParameters["propertytested"] = _testedPropertyName;
rule.ValidationParameters["allowequalvalues"] = _allowEqualValues;
yield return rule;
}
}
您可以使用它像這樣:
public Decimal MyProperty1 { get; set; }
[GreaterThanAttribute("MyProperty1", typeof(Strings), "Error_String")]
public Decimal MyProperty2 { get; set; }
[GreaterThanAttribute("MyProperty2", typeof(Strings), "Error_String")]
public Decimal MyProperty3 { get; set; }
,以及在客戶端,您可以添加此客戶端驗證:
jQuery.validator.unobtrusive.adapters.add('greaterthan', ['propertytested', 'allowequalvalues'], function (options) {
options.params["allowequalvalues"] = options.params["allowequalvalues"] === "True" ||
options.params["allowequalvalues"] === "true" ||
options.params["allowequalvalues"] === true ? true : false;
options.rules['greaterthan'] = options.params;
options.messages['greaterthan'] = options.message;
});
jQuery.validator.addMethod("greaterthan", function (value, element, params) {
var properyTestedvalue= $('input[name="' + params.propertytested + '"]').val();
if (!value || !properyTestedvalue) return true;
return (params.allowequalvalues) ? parseFloat(properyTestedvalue) <= parseFloat(value) : parseFloat(properyTestedvalue) < parseFloat(value);
}, '');
看看我的答案,它包括unobtrusivevalidation – sabotero 2015-02-23 17:09:36