MVC 3比较属性不起作用 [英] MVC 3 Compare Attribute Not Working

查看:110
本文介绍了MVC 3比较属性不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用MVC应用程序,该应用程序具有注册表单,我想在其中使用Compare,Required和Regex属性进行电子邮件验证.用户必须输入他们的电子邮件地址,然后确认电子邮件地址,我想使用Compare属性来确保他们输入相同的内容.这是我的模型;

I am working on an MVC app, which has a registration form where I want to use the Compare, Required and Regex attributes for email validation. The user has to enter their email address, and then confirm email address, I want to use the Compare attribute to ensure they enter the same thing. This is my model;

[RegularExpression(@"<REGEX IS HERE>", ErrorMessage = "Please enter a valid e-mail address")]
[Required]
[DataType(DataType.EmailAddress)]
[Display(Name = "Email")]
public string EmailAddress { get; set; }

[Compare("EmailAddress", ErrorMessage = "The email and confirmation email do not match.")]
[RegularExpression(@"<REGEX IS HERE>", ErrorMessage = "Please enter a valid e-mail address")]        
[Required]        
[DataType(DataType.EmailAddress)]        
[Display(Name = "Confirm Email")]        
public string ConfirmEmailAddress { get; set; }

所有其他验证,即required/regex都可以正常工作,但是在Compare的客户端不起作用,它在表单发布时返回并出现错误.

All the other validation i.e. required / regex works fine, but it doesn't work client side for the Compare, it is working as when the form posts it comes back and errors.

我读过很多文章说jquery.validate.unobtrusive和jquery1.5.1中存在错误,但是我所做的一切都无法解决问题.

I have read many articles saying there is a bug in jquery.validate.unobtrusive and jquery1.5.1 but whatever I do doesn't resolve the issue.

我正在这里拔头发,任何帮助将不胜感激!

I am pulling my hair out here, any help would be appreciated!

推荐答案

其他属性可能正在干扰.您真正需要的只是Compare和Display属性.必需,正则表达式,数据类型都是多余的,因为您已经在主数据库上拥有了这些字段,并且如果与主数据库不匹配,则比较将失败.

It could be that other attributes are intefering.. all you really need is the Compare and Display attributes. Required, Regex, DataType are all superfluous, because you already have those fields on the primary, and the compare will fail if it doesn't match the primary.

另一个想法是下载MvcContrib程序包,并使用自动执行电子邮件验证的Email属性,而不是使用Regex.

Another idea is to download the MvcContrib package, and use the Email attribute that automatically does Email validation, rather than use the Regex.

这篇关于MVC 3比较属性不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆