网站从新建设影响收录么,营销网站建设一薇,成华区微信网站建设公司,做网站如何防止被骗关于ASP.NET MVC的验证#xff0c;用起来很特别#xff0c;因为MS的封装#xff0c;使人理解起来很费解。也可能很多人都在Scott Guthrie等人写的一本《ASP.NET MVC 1.0》书中#xff0c;见过NerdDinner项目中对Dinner对象修改和添加的时的数据验证。但有许多封装的地方用起来很特别因为MS的封装使人理解起来很费解。也可能很多人都在Scott Guthrie等人写的一本《ASP.NET MVC 1.0》书中见过NerdDinner项目中对Dinner对象修改和添加的时的数据验证。但有许多封装的地方不知道是怎样的工作原理今天研究了拿出来给大家分享一下。 数据库还是上一篇blog中的库与表同样的方法来创建news表的实体类在自动生成的news这个实体类中我们发现有一个特殊的分部方法 partial void OnValidate(System.Data.Linq.ChangeAction action);?xml:namespace prefix o ns urn:schemas-microsoft-com:office:office / 这个方法没有实现我们根据C#的语法知道如果分部类中的分部方法没有实现的话调用和定议的地方都不会起什么作用。现在我们要去完善这个方法让它“用”起来。 首先人产在Models中创建news类的另一部分代码如下 public partial class news { partial void OnValidate(System.Data.Linq.ChangeAction action) { if (!IsValid) { throw new ApplicationException(验证内容项出错); } } public bool IsValid { get { return (GetRuleViolations().Count() 0); } } public IEnumerableRuleViolation GetRuleViolations() { if (String.IsNullOrEmpty(this.title .Trim () )) yield return new RuleViolation(题目步能为空, 题目); if (String.IsNullOrEmpty(this.contents .Trim ())) yield return new RuleViolation(内容不能为空, 内容); yield break; } } /// summary /// 规则信息类 /// /summary public class RuleViolation { public string ErrorMessage { get; private set; } public string PropertyName { get; private set; } public RuleViolation(string errorMessage) { ErrorMessage errorMessage; } public RuleViolation(string errorMessage, string propertyName) { ErrorMessage errorMessage; PropertyName propertyName; } } 在这里给出这么多代码其实是提前有设计的因为从业务角度考虑还不应该写这部分代码。RuleViolation类很简单就是一个包括了两个属性的类这个类的结构设计是根据后面的ModelState.AddModelError主法来设计的。 在news分部类中有一个IsValid的属性这个属性是bool类型的返回值取决于GetRuleViolations这个方法这个方法返回值是一个IEnumerableRuleViolation类型的IEnumerable是通过news的几个属性是否为空来生成跌代的。如果title或contents为Null或””就返回跌代。其实真正的用户数据的验证就是在这里实现用户的数据的对与错就是一个逻辑只要用户数据不符合规则就可以 “yield return new RuleViolation(错误标识,错误提示信息)”;这里的错误码提示信息是显示到客户端的所以要处理好友好的提示。 现在验证用户数据生成错误列表的工作都做完了但关键是怎么能让用户提交数据时调用OnValidate。这个问题先放一下请记住上面的代码只要在用户提交数据时调用OnValidate这样就能得到错误集合。 现在让我们来处理Cotroller和View层在Cotroller层首先来添加index这个Action代码如下 public ActionResult Index() { var NewsList DCDC.news.Select(newssnewss); return View(NewsList ); } 这个Action返回所有news表中的记录。 对应的View如下 % Page Title LanguageC# MasterPageFile~/Views/Shared/Site.Master InheritsSystem.Web.Mvc.ViewPageIEnumerableMvcCompany.Models.news % asp:Content IDContent1 ContentPlaceHolderIDTitleContent runatserver Index /asp:Content asp:Content IDContent2 ContentPlaceHolderIDMainContent runatserver h2Index/h2 table tr th/th th ID /th th title /th th datetimes /th th contents /th th IsValid /th /tr % foreach (var item in Model) { % tr td % Html.ActionLink(Edit, Edit, new { iditem.ID }) % | % Html.ActionLink(Details, Details, new { iditem.ID })% /td td % Html.Encode(item.ID) % /td td % Html.Encode(item.title) % /td td % Html.Encode(String.Format({0:g}, item.datetimes)) % /td td % Html.Encode(item.contents) % /td td % Html.Encode(item.IsValid) % /td /tr % } % /table p % Html.ActionLink(Create New, Create) % /p /asp:Content 代码中需要我们注意是的 % Html.ActionLink(Edit, Edit, new { iditem.ID }) %