2011-07-15 208 views
3

这里发生了一些奇怪的事情。MVC 3客户端比较验证

我有一个基本形式:

<% using (Html.BeginForm()) 
     { %> 
     <%: Html.LabelFor(model => model.user.email) %> 
     <%: Html.TextBoxFor(model => model.user.email) %> 
     <%: Html.ValidationMessageFor(model => model.user.email) %> 
     <br /> 
     <%: Html.LabelFor(model => model.user.password) %> 
     <%: Html.PasswordFor(model => model.user.password) %> 
     <%: Html.ValidationMessageFor(model => model.user.password) %> 
     <br /> 
     <%: Html.LabelFor(model => model.user.confirmPassword) %> 
     <%: Html.PasswordFor(model => model.user.confirmPassword) %> 
     <%: Html.ValidationMessageFor(model => model.user.confirmPassword) %> 
     <br /> 
     <input type="submit" value="Submit" /> 
    <% } %> 

头有以下几点:

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script> 
<link rel="Stylesheet" type="text/css" href="/Content/Site.css" /> 
<script src="/Scripts/jquery.validate.min.js" type="text/javascript"></script> 
<script src="/Scripts/jquery.validate.unobtrusive.min.js" type="text/javascript"></script> 

视图模型是这样的:

public class RegisterViewModel 
{ 
    public RegisterUser user { get; set; } 
} 

public class RegisterUser 
{ 
    [Required] 
    [Display(Name = "Email Address")] 
    public string email { get; set; } 

    [Required] 
    [Display(Name = "Password")] 
    public string password { get; set; } 

    [Required] 
    [Display(Name = "Confirm Password")] 
    [Compare("password")] 
    public string confirmPassword { get; set; } 
} 

密码和confirmPassword之间的比较验证总是说:
'确认密码'和'密码'不匹配。
甚至当我知道他们这样做符合

这里的怪部分:当我从视图页面中删除第一场,一切正常。

所以当它只是

<% using (Html.BeginForm()) 
    { %> 
    <%: Html.LabelFor(model => model.user.password) %> 
    <%: Html.PasswordFor(model => model.user.password) %> 
    <%: Html.ValidationMessageFor(model => model.user.password) %> 
    <br /> 
    <%: Html.LabelFor(model => model.user.confirmPassword) %> 
    <%: Html.PasswordFor(model => model.user.confirmPassword) %> 
    <%: Html.ValidationMessageFor(model => model.user.confirmPassword) %> 
    <br /> 
    <input type="submit" value="Submit" /> 
<% } %> 

这一切完美的作品。

任何想法?

谢谢。

+0

你能否详细说明你想达到的目标? – saarthak

+1

@Calvin,我与剃刀有同样的问题。显然,数据验证与您的密码字段相匹配,如下所示:* .Password,它与RegisterUser的所有实例相匹配,而不是RegisterUser.Password。 –

回答

5

这是在客户端验证脚本错误:jquery.validate.unobtrusive.js

在行〜284,你会发现这一点:

element = $(options.form).find(":input[name=" + fullOtherName + "]")[0]; 

它改成这样:

element = $(options.form).find(":input[name='" + fullOtherName + "']")[0]; 

name属性需要单引号。

+2

谢谢你!你的答案是100%正确的! –

-1

是不是因为你正在使用

<%: 

不是

<%= 

我不知道从佣工的输出需要进一步泄漏。

+0

使用<%=与<%时无差异: – Calvin

+1

您的答案错了! –