我刚刚开始使用ASP.NET MVC 2,并且正在使用Validation.
假设我有2个属性:
>密码1
>密码2
我想要求它们都被填写,并要求在模型有效之前两者都相同.
我有一个名为“NewUser”的简单类.
我该如何实现?我已经阅读了ValidationAttribute,并了解这一点.但我不知道如何使用它来实现比较两个或更多属性与eathother的验证.
提前致谢!
以下解决方案的问题:
当这应用于应用程序,并且ModelBinder运行模型的验证时,则存在一个问题:
如果属性级ValidationAttribute包含错误,则不验证类级别ValidationAttribute.我还没有找到解决这个问题的方法.
解决方法@H_502_25@
Visual Studio的默认ASP.NET MVC 2模板包含您需要的确切验证属性.从AccountModels.cs文件粘贴:
[AttributeUsage(AttributeTargets.Class,AllowMultiple = true,Inherited = true)]
public sealed class PropertiesMustMatchAttribute : ValidationAttribute {
private const string _defaultErrorMessage =
"'{0}' and '{1}' do not match.";
private readonly object _typeId = new object();
public PropertiesMustMatchAttribute(string originalProperty,string confirmProperty)
: base(_defaultErrorMessage) {
OriginalProperty = originalProperty;
ConfirmProperty = confirmProperty;
}
public string ConfirmProperty { get; private set; }
public string OriginalProperty { get; private set; }
public override object TypeId {
get {
return _typeId;
}
}
public override string FormatErrorMessage(string name) {
return String.Format(CultureInfo.CurrentUICulture,ErrorMessageString,OriginalProperty,ConfirmProperty);
}
public override bool IsValid(object value) {
PropertyDescriptorCollection properties =
TypeDescriptor.GetProperties(value);
object originalValue = properties.Find(OriginalProperty,true /* ignoreCase */).GetValue(value);
object confirmValue = properties.Find(ConfirmProperty,true /* ignoreCase */).GetValue(value);
return Object.Equals(originalValue,confirmValue);
}
}
如何使用 :
[PropertiesMustMatch("Password","ConfirmPassword",ErrorMessage = "The password and confirmation password do not match.")]
class NewUser {
[required]
[DataType(DataType.Password)]
[DisplayName("Password")]
public string Password { get; set; }
[required]
[DataType(DataType.Password)]
[DisplayName("Confirm password")]
public string ConfirmPassword { get; set; }
}
[AttributeUsage(AttributeTargets.Class,AllowMultiple = true,Inherited = true)] public sealed class PropertiesMustMatchAttribute : ValidationAttribute { private const string _defaultErrorMessage = "'{0}' and '{1}' do not match."; private readonly object _typeId = new object(); public PropertiesMustMatchAttribute(string originalProperty,string confirmProperty) : base(_defaultErrorMessage) { OriginalProperty = originalProperty; ConfirmProperty = confirmProperty; } public string ConfirmProperty { get; private set; } public string OriginalProperty { get; private set; } public override object TypeId { get { return _typeId; } } public override string FormatErrorMessage(string name) { return String.Format(CultureInfo.CurrentUICulture,ErrorMessageString,OriginalProperty,ConfirmProperty); } public override bool IsValid(object value) { PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(value); object originalValue = properties.Find(OriginalProperty,true /* ignoreCase */).GetValue(value); object confirmValue = properties.Find(ConfirmProperty,true /* ignoreCase */).GetValue(value); return Object.Equals(originalValue,confirmValue); } }
如何使用 :
[PropertiesMustMatch("Password","ConfirmPassword",ErrorMessage = "The password and confirmation password do not match.")] class NewUser { [required] [DataType(DataType.Password)] [DisplayName("Password")] public string Password { get; set; } [required] [DataType(DataType.Password)] [DisplayName("Confirm password")] public string ConfirmPassword { get; set; } }