這裏發生了一些奇怪的事情。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" />
<% } %>
這一切完美的作品。
任何想法?
謝謝。
你能否詳細說明你想達到的目標? – saarthak
@Calvin,我與剃刀有同樣的問題。顯然,數據驗證與您的密碼字段相匹配,如下所示:* .Password,它與RegisterUser的所有實例相匹配,而不是RegisterUser.Password。 –