asp.net mvc 之 asp.net mvc 3.0 新特性之 Model:
- 通过 Data Annotations 与 jQuery 的结合实现服务端和客户端的双重验证
- 双重验证中,使客户端实现远程的异步验证
- 自定义 Data Annotations 与 jQuery,以实现自定义的双重验证
示例 1、Model 中通过 Data Annotations 与 jQuery 的结合实现服务端和客户端的双重验证 Web.config
< add key = "ClientValidationEnabled" value = "true" /> |
< add key = "UnobtrusiveJavaScriptEnabled" value = "true" /> |
User.cs
using System.Collections.Generic; |
using System.ComponentModel; |
using System.ComponentModel.DataAnnotations; |
public int ID { get ; set ; } |
[Required(ErrorMessage = "名字不能为空" )] |
public string Name { get ; set ; } |
[Required(ErrorMessage = "密码不能为空" )] |
public string Password { get ; set ; } |
[Required(ErrorMessage = "确认密码不能为空" )] |
[Compare( "Password" , ErrorMessage= "两次密码输入不一致" )] |
public string ConfirmPassword { get ; set ; } |
public DateTime DateOfBirth { get ; set ; } |
public string Comment { get ; set ; } |
ValidationDemoController.cs
using System.Collections.Generic; |
namespace MVC30.Controllers |
public class ValidationDemoController : Controller |
public ActionResult Validation_DataAnnotations() |
var user = new User { ID = 1, Name = "webabcd" , DateOfBirth = new DateTime(1980, 2, 14), Comment = "<b>mvp</b>" }; |
public ActionResult Validation_DataAnnotations(User user) |
Validation_DataAnnotations.cshtml
ViewBag.Title = "Validation_DataAnnotations"; |
< h2 >ClientValidation</ h2 > |
< script src = "@Url.Content(" ~/Scripts/jquery.validate.min.js")" type = "text/javascript" ></ script > |
< script src = "@Url.Content(" ~/Scripts/jquery.validate.unobtrusive.min.js")" type = "text/javascript" ></ script > |
在 MVC3 中实现客户端验证,不需要添加以下代码 |
@{ Html.EnableClientValidation(); } |
@using (Html.BeginForm()) |
< div class = "editor-label" > |
@Html.LabelFor(model => model.Name) |
< div class = "editor-field" > |
@Html.EditorFor(model => model.Name) |
@Html.ValidationMessageFor(model => model.Name) |
< input type = "submit" value = "Create" /> |
2、Model 中通过 Data Annotations 与 jQuery 的结合实现服务端和客户端的双重验证,其中客户端可以实现远程的异步验证 User.cs
using System.Collections.Generic; |
using System.ComponentModel; |
using System.ComponentModel.DataAnnotations; |
public int ID { get ; set ; } |
[Required(ErrorMessage = "名字不能为空" )] |
[Remote( "CheckUserNameExists" , "ValidationDemo" , ErrorMessage = "名字已存在" )] |
public string Name { get ; set ; } |
[Required(ErrorMessage = "密码不能为空" )] |
public string Password { get ; set ; } |
[Required(ErrorMessage = "确认密码不能为空" )] |
[Compare( "Password" , ErrorMessage= "两次密码输入不一致" )] |
public string ConfirmPassword { get ; set ; } |
public DateTime DateOfBirth { get ; set ; } |
public string Comment { get ; set ; } |
ValidationDemoController.cs
using System.Collections.Generic; |
namespace MVC30.Controllers |
public class ValidationDemoController : Controller |
public ActionResult Validation_Remote() |
var user = new User { ID = 1, Name = "webabcd" , DateOfBirth = new DateTime(1980, 2, 14), Comment = "<b>mvp</b>" }; |
public ActionResult Validation_Remote(User user) |
public ActionResult CheckUserNameExists( string name) |
return Json(name != "webabcd" , JsonRequestBehavior.AllowGet); |
Validation_Remote.cshtml
ViewBag.Title = "Validation_Remote"; |
< h2 >ClientValidation</ h2 > |
< script src = "@Url.Content(" ~/Scripts/jquery.validate.min.js")" type = "text/javascript" ></ script > |
< script src = "@Url.Content(" ~/Scripts/jquery.validate.unobtrusive.min.js")" type = "text/javascript" ></ script > |
@using (Html.BeginForm()) |
< div class = "editor-label" > |
@Html.LabelFor(model => model.Name) |
< div class = "editor-field" > |
@Html.EditorFor(model => model.Name) |
@Html.ValidationMessageFor(model => model.Name) |
< input type = "submit" value = "Create" /> |
3、Model 中使用更多的 Data Annotations 以及实现自定义的 Data Annotations 和自定义 jQuery 的相关逻辑 User.cs
using System.Collections.Generic; |
using System.ComponentModel; |
using System.ComponentModel.DataAnnotations; |
public int ID { get ; set ; } |
[Required(ErrorMessage = "名字不能为空" )] |
[Remote( "CheckUserNameExists" , "ValidationDemo" , ErrorMessage = "名字已存在" )] |
public string Name { get ; set ; } |
[Required(ErrorMessage = "密码不能为空" )] |
[Integer(ErrorMessage = "密码必须是整型" )] |
public string Password { get ; set ; } |
[Required(ErrorMessage = "确认密码不能为空" )] |
[Compare( "Password" , ErrorMessage= "两次密码输入不一致" )]
|
|
请发表评论