自定义验证属性 MVC2
我有一个自定义验证属性,用于检查两个属性是否具有相同的值(例如密码和重新输入密码):
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
public class EqualToPropertyAttribute : ValidationAttribute
{
public string CompareProperty { get; set; }
public EqualToPropertyAttribute(string compareProperty)
{
CompareProperty = compareProperty;
ErrorMessage = string.Format(Messages.EqualToError, compareProperty);
}
public override bool IsValid(object value)
{
if (value == null)
{
return true;
}
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(value);
var property = properties.Find(CompareProperty, true);
var comparePropertyValue = property.GetValue(value).ToString();
return comparePropertyValue == value.ToString();
}
}
我有一个视图模型类,其中包含注册表单的所有字段,如下所示:
public class SignUpViewModel
{
[Required]
[StringLength(100)]
public string Username { get; set; }
[Required]
[Password]
public string Password { get; set; }
[Required]
[DisplayText("RetypePassword")]
[EqualToProperty("Password")]
public string RetypePassword { get; set; }
[Required]
[StringLength(50)]
[DisplayText("FirstName")]
public string FirstName { get; set; }
[Required]
[StringLength(100)]
[DisplayText("LastName")]
public string LastName { get; set; }
[Required]
[DisplayText("SecurityQuestion")]
public int SecurityQuestionID { get; set; }
public IEnumerable<SelectListItem> SecurityQuestions { get; set; }
[Required]
[StringLength(50)]
public string Answer { get; set; }
}
下面是我的控制器代码:
public virtual ActionResult Index()
{
var signUpViewModel = new SignUpViewModel();
signUpViewModel.SecurityQuestions = new SelectList(questionRepository.GetAll(),"SecurityQuestionID", "Question");
return View(signUpViewModel);
}
[HttpPost]
public virtual ActionResult Index(SignUpViewModel viewModel)
{
// Code to save values to database
}
当我输入表单值并点击“提交”时,尝试获取属性描述符 var property =properties.Find(CompareProperty, true); 的代码行返回 null。谁能帮助我理解为什么会发生这种情况?
I have a custom validation attribute which checks to see if two properties have the same values or not (like password and retype password):
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
public class EqualToPropertyAttribute : ValidationAttribute
{
public string CompareProperty { get; set; }
public EqualToPropertyAttribute(string compareProperty)
{
CompareProperty = compareProperty;
ErrorMessage = string.Format(Messages.EqualToError, compareProperty);
}
public override bool IsValid(object value)
{
if (value == null)
{
return true;
}
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(value);
var property = properties.Find(CompareProperty, true);
var comparePropertyValue = property.GetValue(value).ToString();
return comparePropertyValue == value.ToString();
}
}
I have a view model class which has all the fields for the signup form as follows:
public class SignUpViewModel
{
[Required]
[StringLength(100)]
public string Username { get; set; }
[Required]
[Password]
public string Password { get; set; }
[Required]
[DisplayText("RetypePassword")]
[EqualToProperty("Password")]
public string RetypePassword { get; set; }
[Required]
[StringLength(50)]
[DisplayText("FirstName")]
public string FirstName { get; set; }
[Required]
[StringLength(100)]
[DisplayText("LastName")]
public string LastName { get; set; }
[Required]
[DisplayText("SecurityQuestion")]
public int SecurityQuestionID { get; set; }
public IEnumerable<SelectListItem> SecurityQuestions { get; set; }
[Required]
[StringLength(50)]
public string Answer { get; set; }
}
Below is my controller code:
public virtual ActionResult Index()
{
var signUpViewModel = new SignUpViewModel();
signUpViewModel.SecurityQuestions = new SelectList(questionRepository.GetAll(),"SecurityQuestionID", "Question");
return View(signUpViewModel);
}
[HttpPost]
public virtual ActionResult Index(SignUpViewModel viewModel)
{
// Code to save values to database
}
When I enter the form values and hit submit the line of code which tries to get the property descriptor var property = properties.Find(CompareProperty, true); is returning null. Can anyone help me understand why this is happening?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
因为
IsValid()
的object value
参数不是整个模型,而只是您的string RetypePassword
。它需要是影响整个模型对象的属性,而不仅仅是属性。
尽管我建议您使用
PropertiesMustMatchAttribute
。编辑
该属性实际上不是 ASP.NET MVC2 框架的一部分,而是在默认的 MVC 2 项目模板中定义。
顺便说一句,MVC 3 有一个
CompareAttribute
这正是你想要的。Because the
object value
parameter ofIsValid()
is not the entire model, but just yourstring RetypePassword
.It would need to be an attribute that affects the whole model object, and not just a property.
Although I would suggest you to use the
PropertiesMustMatchAttribute
.Edit
That attribute is actually not part of the ASP.NET MVC2 framework, but defined in the default MVC 2 project template.
On a side note, MVC 3 has a
CompareAttribute
that does exactly what you want.我不确定为什么你的代码不起作用,但通过
GetType()
你可以获得预期的结果:I'm not sure why your code doesn't work, but through
GetType()
you can get the expected result:根据 http://msdn.microsoft.com/en-us/library/ ybh0y4fd.aspx TypeDecroptor.GetProperties“返回指定组件的属性集合。”它还接着说:
所以在我看来,这并不是获取类属性的正确方法。我认为@Pieter 的方法更符合您真正寻找的方法。
According to http://msdn.microsoft.com/en-us/library/ybh0y4fd.aspx the TypeDescroptor.GetProperties "Returns the collection of properties for a specified component." it also goes on to say:
So it appears to me that this isn't really the right method to be using to get the properties of a class. I think @Pieter's method is more what you're really looking for.