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