ASP.NET MVC 音樂市集 – 6. 使用 DataAnnotations 進行模型驗證

來源:互聯網
上載者:User
文章目錄
  • 為專輯表單增加驗證
  • 測試用戶端驗證

轉自http://www.cnblogs.com/haogj/archive/2011/11/16/2251920.html

在前面的建立專輯與編輯專輯的表單中存在一個問題:我們沒有進行任何驗證。欄位的內容可以不輸入,或者在價格的欄位中輸入一些字元,在執行程式的時候,這些錯誤會導致資料庫儲存過程中出現錯誤,我們將會看到來自資料庫的錯誤資訊。

通過為模型類增加資料描述的 DataAnnotations ,我們可以容易地為應用程式增加驗證的功能。DataAnnotations  允許我們描述希望應用在模型屬性上的驗證規則,ASP.NET MVC 將會使用這些 DataAnnotations ,然後將適當的驗證資訊返回給使用者。

為專輯表單增加驗證

我們將會使用下列的 DataAnnotations

  • Required 必須 – 表示這個屬性是必須提供內容的欄位
  • DisplayName 顯示名 – 定義表單欄位的提示名稱
  • StringLength 字串長度 – 定義字串類型的屬性的最大長度
  • Range 範圍 – 為數字類型的屬性提供最大值和最小值
  • Bind 綁定 – 列出在將請求參數綁定到模型的時候,包含和不包含的欄位
  • ScaffoldColumn 支架列 - 在編輯表單的時候,需要隱藏起來的的字元

注意:更多關於模型驗證的資訊,請參考:http://msdn.microsoft.com/zh-cn/library/ee256141%28VS.100%29.aspx

開啟 Album 類,首先增加下面的 using 語句,這些語句引用了 DataAnnotations  使用的命名空間。

using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;

 

然後,更新屬性,增加顯示和驗證的 DataAnnotations

namespace MvcMusicStore.Models
{
[Bind(Exclude = "AlbumId")]
public class Album
{
[ScaffoldColumn(false)]
public int AlbumId { get; set; }
[DisplayName("Genre")]
public int GenreId { get; set; }
[DisplayName("Artist")]
public int ArtistId { get; set; }
[Required(ErrorMessage = "An Album Title is required")]
[StringLength(160)]
public string Title { get; set; }
[Required(ErrorMessage = "Price is required")]
[Range(0.01, 100.00,
ErrorMessage = "Price must be between 0.01 and 100.00")]
public decimal Price { get; set; }
[DisplayName("Album Art URL")]
[StringLength(1024)]
public string AlbumArtUrl { get; set; }
public virtual Genre Genre { get; set; }
public virtual Artist Artist { get; set; }
}
}

 

然後,將專輯 Album 的屬性 Genre 和 Artist 設定為虛擬 virtual ,這將會使 EF-Code First 使用消極式載入。

public virtual Genre Genre { get; set; }
public virtual Artist Artist { get; set; }

 

為專緝修改完成之後,我們的建立和編輯介面立即就會驗證欄位,並且使用我們提供的顯示名稱,例如  AlbumArtUrl 將會成為 Album Art URL 等等。運行程式,瀏覽 /StoreManager/Create.

下一步,我們特意輸入一些破壞驗證規則的資料,在價格欄位中輸入 0, 將標題欄位的內容保留為空白,當我們點擊建立的時候,我們將會看到表單中不符合驗證規則的欄位顯示了驗證的錯誤提示資訊。

測試用戶端驗證

對於應用程式來說,伺服器端驗證非常重要,因為使用者可能繞過了用戶端驗證,實際上,Web 頁面僅僅實現伺服器端驗證存在三個顯著的問題:

  1. 在提交表單的時候,使用者必須等待,驗證在伺服器端進行,需要將驗證的結果發送回瀏覽器。
  2. 使用者不能在輸入錯誤的時候立即得到回應,以便通過驗證規則的檢查。
  3. 我們把可以在瀏覽器完成的工作交給了伺服器,浪費了伺服器的資源。 

幸運的是,ASP.NET MVC3 支架模板還提供了內建的用戶端驗證,不需要我們做額外的工作就可以使用。

在必須輸入的標題欄位中輸入一個字元,驗證的錯誤提示資訊立即就消失了。

這裡,我們要注意幾點:

頁面中已經引用了 jQuery 的指令碼。

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

在 web.config 中,已經預設支援了用戶端驗證。

  <appSettings>
<add key="ClientValidationEnabled" value="true"/>
<add key="UnobtrusiveJavaScriptEnabled" value="true"/>
</appSettings>

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.