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= "两次密码输入不一致" )]
|
-
六六分期app的软件客服如何联系?不知道吗?加qq群【895510560】即可!标题:六六分期
阅读:18013|2023-10-27
-
今天小编告诉大家如何处理win10系统火狐flash插件总是崩溃的问题,可能很多用户都不知
阅读:9590|2022-11-06
-
今天小编告诉大家如何对win10系统删除桌面回收站图标进行设置,可能很多用户都不知道
阅读:8140|2022-11-06
-
今天小编告诉大家如何对win10系统电脑设置节能降温的设置方法,想必大家都遇到过需要
阅读:8522|2022-11-06
-
我们在使用xp系统的过程中,经常需要对xp系统无线网络安装向导设置进行设置,可能很多
阅读:8425|2022-11-06
-
今天小编告诉大家如何处理win7系统玩cf老是与主机连接不稳定的问题,可能很多用户都不
阅读:9327|2022-11-06
-
电脑对日常生活的重要性小编就不多说了,可是一旦碰到win7系统设置cf烟雾头的问题,很
阅读:8388|2022-11-06
-
我们在日常使用电脑的时候,有的小伙伴们可能在打开应用的时候会遇见提示应用程序无法
阅读:7823|2022-11-06
-
今天小编告诉大家如何对win7系统打开vcf文件进行设置,可能很多用户都不知道怎么对win
阅读:8377|2022-11-06
-
今天小编告诉大家如何对win10系统s4开启USB调试模式进行设置,可能很多用户都不知道怎
阅读:7370|2022-11-06
|
请发表评论