最近做MVC的项目,运用DataAnnotation(using System.ComponentModel.DataAnnotations;)
运用自带的MicrosoftAjax.js,MicrosoftMvcValidation.js
进行客户端的验证,出现了个很有意思的问题,经过多次测试,总算发现了点点端倪
什么都不说了,贴上代码:Models:AccountModels.cs程序自带的代码
1 [PropertiesMustMatch("Password", "ConfirmPassword", ErrorMessage = "密码和确认密码不匹配。")]
2 public class RegisterModel
3 {
4 [Required]
5 [DisplayName("用户名")]
6 public string UserName { get; set; }
7
8 [Required]
9 [ValidatePasswordLength]
10 [DataType(DataType.Password)]
11 [DisplayName("密码")]
12 public string Password { get; set; }
13
14 [Required]
15 [DataType(DataType.Password)]
16 [DisplayName("确认密码")]
17 public string ConfirmPassword { get; set; }
18
19 [Required]
20 [DataType(DataType.EmailAddress)]
21 [RegularExpression(@"^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$", ErrorMessage = "电子邮件格式错误")]
22 [DisplayName("电子邮箱")]
23 public string Email { get; set; }
24 }
2 public class RegisterModel
3 {
4 [Required]
5 [DisplayName("用户名")]
6 public string UserName { get; set; }
7
8 [Required]
9 [ValidatePasswordLength]
10 [DataType(DataType.Password)]
11 [DisplayName("密码")]
12 public string Password { get; set; }
13
14 [Required]
15 [DataType(DataType.Password)]
16 [DisplayName("确认密码")]
17 public string ConfirmPassword { get; set; }
18
19 [Required]
20 [DataType(DataType.EmailAddress)]
21 [RegularExpression(@"^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$", ErrorMessage = "电子邮件格式错误")]
22 [DisplayName("电子邮箱")]
23 public string Email { get; set; }
24 }
Views:Register.aspx【程序自带的代码,多添加了Table标签,报错就出在Table标签中】
1 <%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<MVC验证码错误.Models.RegisterModel>" %>
2
3 <asp:Content ID="registerTitle" ContentPlaceHolderID="TitleContent" runat="server">
4 注册
5 </asp:Content>
6 <asp:Content ID="registerContent" ContentPlaceHolderID="MainContent" runat="server">
7 <h2>
8 创建新帐户</h2>
9 <p>
10 使用以下表单创建新帐户。
11 </p>
12 <p>
13 密码的长度至少为
14 <%: ViewData["PasswordLength"] %>
15 个字符。
16 </p>
17 <% Html.EnableClientValidation(); %>
18 <% using (Html.BeginForm())
19 { %>
20 <%: Html.ValidationSummary(true, "帐户创建不成功。请更正错误并重试。") %>
21 <div>
22 <fieldset>
23 <legend>帐户信息</legend>
24 <div class="editor-label">
25 <%: Html.LabelFor(m => m.UserName) %>
26 </div>
27 <div class="editor-field">
28 <%: Html.TextBoxFor(m => m.UserName) %>
29 <%: Html.ValidationMessageFor(m => m.UserName) %>
30 </div>
31 <div class="editor-label">
32 <%: Html.LabelFor(m => m.Email) %>
33 </div>
34 <div class="editor-field">
35 <%:Html.LabelFor(m=>m.Email) %>
36 <%: Html.TextBoxFor(m => m.Email) %>
37 <%: Html.ValidationMessageFor(m => m.Email) %>
38 </div>
39 <div class="editor-label">
40 <%: Html.LabelFor(m => m.Password) %>
41 </div>
42 <div class="editor-field">
43 <%: Html.PasswordFor(m => m.Password) %>
44 <%: Html.ValidationMessageFor(m => m.Password) %>
45 </div>
46 <div class="editor-label">
47 <%: Html.LabelFor(m => m.ConfirmPassword) %>
48 </div>
49 <div class="editor-field">
50 <%: Html.PasswordFor(m => m.ConfirmPassword) %>
51 <%: Html.ValidationMessageFor(m => m.ConfirmPassword) %>
52 </div>
53 <p>
54 <input type="submit" value="注册" />
55 </p>
56 </fieldset>
57 <table><!--用这样的验证的话,容易报错-->
58 <tr>
59 <td><%: Html.LabelFor(m => m.Email) %></td>
60 <td><%: Html.TextBoxFor(m => m.Email) %></td>
61 <td><%: Html.ValidationMessageFor(m => m.Email) %></td>
62 </tr>
63 </table>
64 </div>
65 <% } %>
66 </asp:Content>
2
3 <asp:Content ID="registerTitle" ContentPlaceHolderID="TitleContent" runat="server">
4 注册
5 </asp:Content>
6 <asp:Content ID="registerContent" ContentPlaceHolderID="MainContent" runat="server">
7 <h2>
8 创建新帐户</h2>
9 <p>
10 使用以下表单创建新帐户。
11 </p>
12 <p>
13 密码的长度至少为
14 <%: ViewData["PasswordLength"] %>
15 个字符。
16 </p>
17 <% Html.EnableClientValidation(); %>
18 <% using (Html.BeginForm())
19 { %>
20 <%: Html.ValidationSummary(true, "帐户创建不成功。请更正错误并重试。") %>
21 <div>
22 <fieldset>
23 <legend>帐户信息</legend>
24 <div class="editor-label">
25 <%: Html.LabelFor(m => m.UserName) %>
26 </div>
27 <div class="editor-field">
28 <%: Html.TextBoxFor(m => m.UserName) %>
29 <%: Html.ValidationMessageFor(m => m.UserName) %>
30 </div>
31 <div class="editor-label">
32 <%: Html.LabelFor(m => m.Email) %>
33 </div>
34 <div class="editor-field">
35 <%:Html.LabelFor(m=>m.Email) %>
36 <%: Html.TextBoxFor(m => m.Email) %>
37 <%: Html.ValidationMessageFor(m => m.Email) %>
38 </div>
39 <div class="editor-label">
40 <%: Html.LabelFor(m => m.Password) %>
41 </div>
42 <div class="editor-field">
43 <%: Html.PasswordFor(m => m.Password) %>
44 <%: Html.ValidationMessageFor(m => m.Password) %>
45 </div>
46 <div class="editor-label">
47 <%: Html.LabelFor(m => m.ConfirmPassword) %>
48 </div>
49 <div class="editor-field">
50 <%: Html.PasswordFor(m => m.ConfirmPassword) %>
51 <%: Html.ValidationMessageFor(m => m.ConfirmPassword) %>
52 </div>
53 <p>
54 <input type="submit" value="注册" />
55 </p>
56 </fieldset>
57 <table><!--用这样的验证的话,容易报错-->
58 <tr>
59 <td><%: Html.LabelFor(m => m.Email) %></td>
60 <td><%: Html.TextBoxFor(m => m.Email) %></td>
61 <td><%: Html.ValidationMessageFor(m => m.Email) %></td>
62 </tr>
63 </table>
64 </div>
65 <% } %>
66 </asp:Content>
如果table标签存在的话,在table标签的Email地址栏输入信息的时候,只要Email地址一输入错误,就会被卡死,就必须被迫关闭浏览器了
所以:个人建议,如果运用这个验证的话,尽量少用Table标签,尽量用Div来实现