MVC3----数据注解与验证(2)之 详解Remote验证与Compare验证
***************************************************Remote验证
概要:
如果要实现像用户注册那样,不允许出现重复的账户,就可以用到Remote验证。Remote特性允许利用服务器端的回调函数执行客户端的验证逻辑。它只是在文本框中输入字符的时候向服务器提交get请求,Remote验证只支持输入的时候验证,不支持提交的时候验证,这存在一定的安全隐患。所以我们要在提交的时候也要验证,验证失败了,就添加上ModelError
实现:
-------模型代码:
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Web;usingSystem.Data.Entity;usingSystem.ComponentModel;usingSystem.ComponentModel.DataAnnotations;usingSystem.Web.Mvc;//需要的命名空间namespaceSchoolManageDomw.Models{publicclassSchoolType{[Key]publicvirtualintst_id{get;set;}//要调用的方法控制器名称[Remote("CheckUserName","SchoolType")]//Remote验证publicvirtualstringst_name{get;set;}publicvirtualList<School>Schools{get;set;}}}
-------控制器代码:
需要的名称控件:
using System.Web.Security;
using System.Web.UI;
privateSchoolDBContextdb=newSchoolDBContext();///<summary>///定义一个方法,做唯一判断///</summary>///<paramname="st_name"></param>///<returns></returns>privateboolIsDistinctStName(stringst_name){if(db.SchoolTypes.Where(r=>r.st_name==st_name).ToList().Count>0)returntrue;elsereturnfalse;}///<summary>///被调用的方法///</summary>///<paramname="st_name"></param>///<returns></returns>[OutputCache(Location=OutputCacheLocation.None,NoStore=true)]//加上清除缓存publicJsonResultCheckUserName(stringst_name){if(IsDistinctStName(st_name)){returnJson("用户名不唯一",JsonRequestBehavior.AllowGet);}else{returnJson(true,JsonRequestBehavior.AllowGet);}}[HttpPost]publicActionResultCreate(SchoolTypeschooltype){//提交到服务器做一次判断if(IsDistinctStName(schooltype.st_name)){ModelState.AddModelError("st_name","用户名称不是唯一的");}if(ModelState.IsValid){db.SchoolTypes.Add(schooltype);db.SaveChanges();returnRedirectToAction("Index");}returnView(schooltype);}
-----视图代码:
@modelSchoolManageDomw.Models.SchoolType@{ViewBag.Title="Create";}<h3>Create</h3><scriptsrc="@Url.Content("~/Scripts/jquery.validate.min.js")"type="text/javascript"></script><scriptsrc="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")"type="text/javascript"></script>@using(Html.BeginForm()){@Html.ValidationSummary(true)<fieldset><legend>SchoolType</legend><divclass="editor-label">@Html.LabelFor(model=>model.st_name)</div><divclass="editor-field">@Html.EditorFor(model=>model.st_name)@Html.ValidationMessageFor(model=>model.st_name)</div><p><inputtype="submit"value="Create"/></p></fieldset>}<div>@Html.ActionLink("BacktoList","Index")</div>
***************************************************Compare验证
概要:
如果需要比较验证,比如密码是否输入一致等,就可以用Compare验证
实现:
-----模型代码:
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Web;usingSystem.Data.Entity;usingSystem.ComponentModel;usingSystem.ComponentModel.DataAnnotations;usingSystem.Web.Mvc;namespaceSchoolManageDomw.Models{publicclassSchoolType{[Key]publicvirtualintst_id{get;set;}[Required]//不许为空publicvirtualstringst_name{get;set;}[Compare("st_name")]publicvirtualstringst_nameConfirm{get;set;}publicvirtualList<School>Schools{get;set;}}}
-----视图代码:
@modelSchoolManageDomw.Models.SchoolType@{ViewBag.Title="Create";}<h3>Create</h3><scriptsrc="@Url.Content("~/Scripts/jquery.validate.min.js")"type="text/javascript"></script><scriptsrc="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")"type="text/javascript"></script>@using(Html.BeginForm()){@Html.ValidationSummary(true)<fieldset><legend>SchoolType</legend><divclass="editor-label">@Html.LabelFor(model=>model.st_name)</div><divclass="editor-field">@Html.EditorFor(model=>model.st_name)@Html.ValidationMessageFor(model=>model.st_name)</div><divclass="editor-label">@Html.LabelFor(model=>model.st_nameConfirm)</div><divclass="editor-field">@Html.EditorFor(model=>model.st_nameConfirm)@Html.ValidationMessageFor(model=>model.st_nameConfirm)</div><p><inputtype="submit"value="Create"/></p></fieldset>}<div>@Html.ActionLink("BacktoList","Index")</div>
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。