ASP.NET MVC数据验证(上)
生活随笔
收集整理的這篇文章主要介紹了
ASP.NET MVC数据验证(上)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
關(guān)于ASP.NET MVC的驗(yàn)證,用起來很特別,因?yàn)?/span>MS的封裝,使人理解起來很費(fèi)解。也可能很多人都在Scott Guthrie等人寫的一本《ASP.NET MVC 1.0》書中,見過NerdDinner項(xiàng)目中對(duì)Dinner對(duì)象修改和添加的時(shí)的數(shù)據(jù)驗(yàn)證。但有許多封裝的地方,不知道是怎樣的工作原理,今天研究了,拿出來給大家分享一下。 數(shù)據(jù)庫還是上一篇blog中的庫與表,同樣的方法來創(chuàng)建news表的實(shí)體類,在自動(dòng)生成的news這個(gè)實(shí)體類中,我們發(fā)現(xiàn)有一個(gè)特殊的分部方法: partial void OnValidate(System.Data.Linq.ChangeAction action);<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /> 這個(gè)方法沒有實(shí)現(xiàn),我們根據(jù)C#的語法知道,如果分部類中的分部方法,沒有實(shí)現(xiàn)的話,調(diào)用和定議的地方都不會(huì)起什么作用。現(xiàn)在,我們要去完善這個(gè)方法,讓它“用”起來。 首先,人產(chǎn)在Models中創(chuàng)建news類的另一部分,代碼如下: ??? public partial? class news ??? { ??????? partial void OnValidate(System.Data.Linq.ChangeAction action) ??????? { ??????????? if (!IsValid) ??????????? { ??????????????? throw new ApplicationException("驗(yàn)證內(nèi)容項(xiàng)出錯(cuò)!"); ??????????? } ??????? } ??????? public bool IsValid ??????? { ??????????? get { return (GetRuleViolations().Count() == 0); } ??????? } ??????? public IEnumerable<RuleViolation> GetRuleViolations() ??????? { ??????????? if (String.IsNullOrEmpty(this.title .Trim () )) ??????????????? yield return new RuleViolation("題目步能為空!", "題目"); ??????????? if (String.IsNullOrEmpty(this.contents .Trim ())) ??????????????? yield return new RuleViolation("內(nèi)容不能為空!", "內(nèi)容");????????? ??????????? yield break; ??????? } ??? } /// <summary> ??? /// 規(guī)則信息類 ??? /// </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; ??????? } ??? } 在這里給出這么多代碼,其實(shí)是提前有設(shè)計(jì)的,因?yàn)閺臉I(yè)務(wù)角度考慮,還不應(yīng)該寫這部分代碼。RuleViolation類很簡單,就是一個(gè)包括了兩個(gè)屬性的類(這個(gè)類的結(jié)構(gòu)設(shè)計(jì)是根據(jù)后面的ModelState.AddModelError主法來設(shè)計(jì)的)。 在news分部類中,有一個(gè)IsValid的屬性,這個(gè)屬性是bool類型的,返回值取決于GetRuleViolations這個(gè)方法,這個(gè)方法返回值是一個(gè)IEnumerable<RuleViolation>類型的,IEnumerable是通過news的幾個(gè)屬性是否為空來生成跌代的。如果title或contents為Null或””,就返回跌代。其實(shí)真正的用戶數(shù)據(jù)的驗(yàn)證就是在這里實(shí)現(xiàn),用戶的數(shù)據(jù)的對(duì)與錯(cuò),就是一個(gè)邏輯,只要用戶數(shù)據(jù)不符合規(guī)則,就可以 “yield return new RuleViolation("錯(cuò)誤標(biāo)識(shí)","錯(cuò)誤提示信息!")”;這里的錯(cuò)誤碼提示信息是顯示到客戶端的,所以要處理好友好的提示。 現(xiàn)在驗(yàn)證用戶數(shù)據(jù),生成錯(cuò)誤列表的工作都做完了,但關(guān)鍵是怎么能讓用戶提交數(shù)據(jù)時(shí),調(diào)用OnValidate。這個(gè)問題,先放一下,請(qǐng)記住,上面的代碼,只要在用戶提交數(shù)據(jù)時(shí),調(diào)用OnValidate,這樣就能得到錯(cuò)誤集合。 現(xiàn)在,讓我們來處理Cotroller和View層,在Cotroller層,首先來添加index這個(gè)Action,代碼如下: public ActionResult Index() ??????? {?????????? ??????????? var NewsList = DCDC.news.Select(newss=>newss); ??????????? return View(NewsList ); ???? } 這個(gè)Action返回所有news表中的記錄。 對(duì)應(yīng)的View如下: <%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<MvcCompany.Models.news>>" %>?
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server"> ???? Index </asp:Content>?
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">?
??? <h2>Index</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 { id=item.ID }) %> | ??????????????? <%= Html.ActionLink("Details", "Details", new { id=item.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 { id=item.ID }) %>總結(jié)
以上是生活随笔為你收集整理的ASP.NET MVC数据验证(上)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: echarts 山东地图_用Python
- 下一篇: 设计模式杂谈(一)——设计模式概述