新建一个Model
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel.DataAnnotations; 4 using System.Linq; 5 using System.Web; 6 7 namespace MvcValidateDemo.Models 8 { 9 public class UserInfo 10 { 11 public int Id { get; set; } 12 [StringLength(5, ErrorMessage = "*长度必须小于5")] 13 [Required(ErrorMessage = "*姓名为必填项")] 14 public string UserName { get; set; } 15 [RegularExpression(@"^/d+$")] 16 [Range(18, 20)] 17 public int Age { get; set; } 18 } 19 } UserInfo
创建UserInfo控制器
1 using MvcValidateDemo.Models; 2 using System; 3 using System.Collections.Generic; 4 using System.Linq; 5 using System.Web; 6 using System.Web.Mvc; 7 8 namespace MvcValidateDemo.Controllers 9 { 10 public class UserInfoController : Controller 11 { 12 // 13 // GET: /UserInfo/ 14 15 public ActionResult Index() 16 { 17 return View(); 18 } 19 20 21 public ActionResult Add() 22 { 23 24 25 return View(); 26 } 27 [HttpPost] 28 public ActionResult Add(UserInfo userInfo) 29 { 30 //ModelState.IsValid==true那么校验就是成功的 31 if (ModelState.IsValid) 32 { 33 34 } 35 return RedirectToAction("Index"); 36 } 37 } 38 } UserInfo
创建强类型前台页面
1 @model MvcValidateDemo.Models.UserInfo 2 3 @{ 4 Layout = null; 5 } 6 7 <!DOCTYPE html> 8 9 <html> 10 <head> 11 <meta name="viewport" content="width=device-width" /> 12 <title>Add</title> 13 </head> 14 <body> 15 <script src="~/Scripts/jquery-1.8.2.min.js"></script> 16 <script src="~/Scripts/jquery.validate.min.js"></script> 17 <script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script> 18 19 @using (Html.BeginForm("Index","UserInfo",FormMethod.Get)) { 20 @Html.AntiForgeryToken() 21 @Html.ValidationSummary(true) 22 23 <fieldset> 24 <legend>UserInfo</legend> 25 26 <div class="editor-label"> 27 @Html.LabelFor(model => model.UserName) 28 </div> 29 <div class="editor-field"> 30 @Html.TextBoxFor(model => model.UserName) 31 @Html.ValidationMessageFor(model => model.UserName) 32 </div> 33 34 <div class="editor-label"> 35 @Html.LabelFor(model => model.Age) 36 </div> 37 <div class="editor-field"> 38 @Html.TextBoxFor(model => model.Age) 39 @Html.ValidationMessageFor(model => model.Age) 40 </div> 41 42 <p> 43 <input type="submit" value="注册" /> 44 </p> 45 </fieldset> 46 } 47 48 <div> 49 @Html.ActionLink("Back to List", "Index") 50 </div> 51 </body> 52 </html> Add
注意事项:
1:在设置全局校验的时候,引用jquery文件
<script src="~/Scripts/jquery-1.8.2.min.js"></script>
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
2:客户端全局校验设置:在webconfig中
<add key="ClientValidationEnabled" value="true" />
如果为false,则整个全局客户端校验则没有了
如果设置当前某个页面的客户端校验,则使用代码:
1 @model MvcValidateDemo.Models.UserInfo 2 3 @{ 4 Layout = null; 5 } 6 7 <!DOCTYPE html> 8 9 <html> 10 <head> 11 <meta name="viewport" content="width=device-width" /> 12 <title>Add</title> 13 <script src="~/Scripts/jquery-1.8.2.min.js"></script> 14 <script src="~/Scripts/jquery.validate.min.js"></script> 15 <script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script> 16 </head> 17 <body> 18 19 @{Html.EnableClientValidation(false);} 20 21 @using (Html.BeginForm("Index", "UserInfo", FormMethod.Get)) 22 { 23 @Html.AntiForgeryToken() 24 @Html.ValidationSummary(true) 25 26 <fieldset> 27 <legend>UserInfo</legend> 28 29 <div class="editor-label"> 30 @Html.LabelFor(model => model.UserName) 31 </div> 32 <div class="editor-field"> 33 @Html.TextBoxFor(model => model.UserName) 34 @Html.ValidationMessageFor(model => model.UserName) 35 </div> 36 37 <div class="editor-label"> 38 @Html.LabelFor(model => model.Age) 39 </div> 40 <div class="editor-field"> 41 @Html.TextBoxFor(model => model.Age) 42 @Html.ValidationMessageFor(model => model.Age) 43 </div> 44 45 <p> 46 <input type="submit" value="注册" /> 47 </p> 48 </fieldset> 49 } 50 51 <div> 52 @Html.ActionLink("Back to List", "Index") 53 </div> 54 </body> 55 </html> @{Html.EnableClientValidation(false);}
其中默认为true,改为false,则当前页面客户端校验失效