使用 DataAnnotations 比较两个模型属性
我将如何编写一个比较两个字段的自定义 ValidationAttribute?这就是常见的“输入密码”、“确认密码”场景。我需要确保两个字段相等并保持一致,我想通过 DataAnnotations 实现验证。
因此,在伪代码中,我正在寻找一种方法来实现如下所示的内容:
public class SignUpModel
{
[Required]
[Display(Name = "Password")]
public string Password { get; set; }
[Required]
[Display(Name = "Re-type Password")]
[Compare(CompareField = Password, ErrorMessage = "Passwords do not match")]
public string PasswordConfirm { get; set; }
}
public class CompareAttribute : ValidationAttribute
{
public CompareAttribute(object propertyToCompare)
{
// ??
}
public override bool IsValid(object value)
{
// ??
}
}
所以问题是,如何编码 [Compare] ValidationAttribute?
How would I go about writing a custom ValidationAttribute that compares two fields? This is the common "enter password", "confirm password" scenario. I need to be sure the two fields are equal and to keep things consistent, I want to implement the validation via DataAnnotations.
So in pseudo-code, I'm looking for a way to implement something like the following:
public class SignUpModel
{
[Required]
[Display(Name = "Password")]
public string Password { get; set; }
[Required]
[Display(Name = "Re-type Password")]
[Compare(CompareField = Password, ErrorMessage = "Passwords do not match")]
public string PasswordConfirm { get; set; }
}
public class CompareAttribute : ValidationAttribute
{
public CompareAttribute(object propertyToCompare)
{
// ??
}
public override bool IsValid(object value)
{
// ??
}
}
So the question is, how do I code the [Compare] ValidationAttribute?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
确保您的项目引用 system.web.mvc v3.xxxxx。
那么你的代码应该是这样的
:
。
。
。
Make sure that your project references system.web.mvc v3.xxxxx.
Then your code should be something like this:
.
.
.
.
ASP.NET MVC 3 Framework 中有一个 CompareAttribute 可以执行此操作。如果您使用 ASP.NET MVC 2 并面向 .Net 4.0,那么您可以查看 ASP.NET MVC 3 源代码中的实现。
There is a CompareAttribute in the ASP.NET MVC 3 Framework that does this. If you are using ASP.NET MVC 2 and targeting .Net 4.0 then you could look at the implementation in the ASP.NET MVC 3 source code.
这是达林答案的较长版本:
和用法:
错误将显示在
@Html.ValidationSummary(false)
This is a longer version of Darin's answer:
and usage:
The error will display in
@Html.ValidationSummary(false)
您可以拥有自定义验证属性并将其应用于模型而不是单个属性。您可以看一下以下示例。
You could have a custom validation attribute and apply it on the model and not on individual properties. Here's an example you might take a look at.
如果你们正在使用 MVC 4,请尝试此代码..它将解决您的错误..
请创建一个元数据类,而不是在部分类实现确认电子邮件属性中。检查下面的代码以获取更多详细信息。
if you guys are using MVC 4 please try this code .. it will solve your error..
please make one Metadataclass than in partial class impliment comfirmemail properties . check the below code for more details.
对于未来关注这个问题的人,我试图编写一个验证属性,如果对象的属性是某个值,它将评估正则表达式。就我而言,如果地址是送货地址,我不希望启用邮政信箱,所以这就是我想到的:
用法
这是验证属性的代码:
For future people looking at this issue, I was trying to write a validation attribute that would evaluate a regex if an object's property were a certain value. In my case, if an address was a shipping address, I didn't want PO Boxes enabled, so this is what I came up with:
Usage
And here's the code for the validation attribute: