自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

詳解ASP.NET MVC 2自定義驗(yàn)證

原創(chuàng)
開發(fā) 后端
ASP.NET MVC 2內(nèi)置支持在服務(wù)器上驗(yàn)證數(shù)據(jù)注釋驗(yàn)證屬性,本文介紹如何使用System.ComponentModel.DataAnnotations中的基礎(chǔ)類構(gòu)建自定義驗(yàn)證屬性

【51CTO獨(dú)家特稿】ASP.NET MVC 2內(nèi)置支持在服務(wù)器上驗(yàn)證數(shù)據(jù)注釋驗(yàn)證屬性,本文介紹如何使用System.ComponentModel.DataAnnotations中的基礎(chǔ)類構(gòu)建自定義驗(yàn)證屬性,關(guān)于ASP.NET MVC 2中數(shù)據(jù)注釋是如何工作的,請(qǐng)參考Brad的博客(http://bradwilson.typepad.com/blog/2009/04/dataannotations-and-aspnet-mvc.html)。

#T#

我會(huì)介紹如何連接到ASP.NET MVC 2的客戶端驗(yàn)證擴(kuò)展,以便你可以在客戶端上運(yùn)行JavaScript驗(yàn)證邏輯。

我將創(chuàng)建一個(gè)PriceAttribute來驗(yàn)證某個(gè)值是否大于指定的價(jià)格,并且這個(gè)價(jià)格必須以99分結(jié)束,因此$20.00是無效的值,$19.99是有效的。下面是這個(gè)屬性的代碼:

  1. public class PriceAttribute : ValidationAttribute {  
  2.   public double MinPrice { getset; }  
  3.       
  4.   public override bool IsValid(object value) {  
  5.     if (value == null) {  
  6.       return true;  
  7.     }  
  8.     var price = (double)value;  
  9.     if (price < MinPrice) {  
  10.       return false;  
  11.     }  
  12.     double cents = price - Math.Truncate(price);  
  13.     if(cents < 0.99 || cents >= 0.995) {  
  14.       return false;  
  15.     }  
  16.          
  17.     return true;  
  18.   }  

注意如果值為空,返回的值是true,這個(gè)屬性不會(huì)驗(yàn)證字段是否需要。我會(huì)在RequiredAttribute中驗(yàn)證值是否需要。它允許我將屬性放在可選的值上,當(dāng)用戶將這個(gè)字段留為空時(shí)顯示一個(gè)錯(cuò)誤。

我們可以創(chuàng)建一個(gè)視圖模型,然后應(yīng)用這個(gè)屬性到模型上進(jìn)行快速測(cè)試,下面是這個(gè)模型的代碼:

  1. public class ProductViewModel {  
  2.   [Price(MinPrice = 1.99)]  
  3.   public double Price { getset; }  
  4.  
  5.   [Required]  
  6.   public string Title { getset; }  

我們?cè)倏焖俚貏?chuàng)建一個(gè)視圖(Index.aspx)顯示和編輯窗體:

  1. <%@ Page Language="C#" Inherits="ViewPage" %> 
  2.  
  3. <% using (Html.BeginForm()) { %> 
  4.  
  5.   <%= Html.TextBoxFor(m => m.Title) %> 
  6.     <%= Html.ValidationMessageFor(m => m.Title) %> 
  7.   <%= Html.TextBoxFor(m => m.Price) %> 
  8.     <%= Html.ValidationMessageFor(m => m.Price) %> 
  9.       
  10.     <input type="submit" /> 
  11. <% } %> 

現(xiàn)在我們只需要一個(gè)有兩個(gè)行為的控制器,一個(gè)編輯視圖,另一個(gè)接收提交的ProductViewModel。

  1. [HandleError]  
  2. public class HomeController : Controller {  
  3.   public ActionResult Index() {  
  4.     return View(new ProductViewModel());  
  5.   }  
  6.  
  7.   [HttpPost]  
  8.   public ActionResult Index(ProductViewModel model) {  
  9.     return View(model);  
  10.   }  

我們還沒有開啟客戶端驗(yàn)證,下面來看看當(dāng)我們查看這個(gè)頁面并提交一些值時(shí)會(huì)發(fā)生什么。

結(jié)果

責(zé)任編輯:彭凡 來源: 51CTO
相關(guān)推薦

2009-08-04 13:35:16

ASP.NET自定義樣

2009-09-18 10:20:26

PRG數(shù)據(jù)驗(yàn)證

2009-07-22 15:27:39

ASP.NET MVC自定義路由

2011-04-19 10:33:16

ASP.NET自定義控

2010-04-30 09:32:49

ASP.NET MVC

2009-08-06 17:13:56

ASP.NET自定義控

2009-07-28 09:32:41

ASP.NET自定義控

2009-08-10 14:16:59

ASP.NET自定義控

2009-04-09 09:51:09

ASP.NETSQL Server 自定義分頁

2010-03-19 09:17:16

ASP.NET MVC

2010-02-03 09:50:58

ASP.NET MVC

2009-07-31 10:23:09

ASP.NET源碼DateTimePic

2009-08-12 14:38:05

ASP.NET Dat

2009-08-10 16:58:45

ASP.NET安裝部署

2009-09-11 09:18:17

ASP.NET MVC

2009-07-24 13:20:44

MVC框架ASP.NET

2009-07-31 12:43:59

ASP.NET MVC

2009-07-29 12:55:44

ASP.NET身份驗(yàn)證

2010-01-18 09:25:33

ASP.NET MVC

2010-10-09 08:41:40

Mono 2.8
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)