淺談MVC數據驗證

1、通常狀況html

對於使用過MVC框架的人來講,對MVC的數據驗證不會陌生,好比,我有一個Model以下:前端

1     public class UserInfo
2     {
3         [Required(ErrorMessage = "UserName不可爲空1111")]
4         public string UserName { get; set; }
5         public string Sex { get; set; }
6         public string Mobile { get; set; }
7         public string Address { get; set; }
8     }

前端:數據庫

 1 @using (Html.BeginForm()) 
 2 {
 3     @Html.AntiForgeryToken()
 4     <div class="form-horizontal">
 5         <h4>UserInfo</h4>
 6         <hr />
 7         @Html.ValidationSummary(true, "", new { @class = "text-danger" })
 8         <div class="form-group">
 9             @Html.LabelFor(model => model.UserName, htmlAttributes: new { @class = "control-label col-md-2" })
10             <div class="col-md-10">
11                 @Html.EditorFor(model => model.UserName, new { htmlAttributes = new { @class = "form-control" } })
12                 @Html.ValidationMessageFor(model => model.UserName, "", new { @class = "text-danger" })
13             </div>
14         </div>
15         <div class="form-group">
16             @Html.LabelFor(model => model.Sex, htmlAttributes: new { @class = "control-label col-md-2" })
17             <div class="col-md-10">
18                 @Html.EditorFor(model => model.Sex, new { htmlAttributes = new { @class = "form-control" } })
19                 @Html.ValidationMessageFor(model => model.Sex, "", new { @class = "text-danger" })
20             </div>
21         </div>
22         <div class="form-group">
23             @Html.LabelFor(model => model.Mobile, htmlAttributes: new { @class = "control-label col-md-2" })
24             <div class="col-md-10">
25                 @Html.EditorFor(model => model.Mobile, new { htmlAttributes = new { @class = "form-control" } })
26                 @Html.ValidationMessageFor(model => model.Mobile, "", new { @class = "text-danger" })
27             </div>
28         </div>
29         <div class="form-group">
30             @Html.LabelFor(model => model.Address, htmlAttributes: new { @class = "control-label col-md-2" })
31             <div class="col-md-10">
32                 @Html.EditorFor(model => model.Address, new { htmlAttributes = new { @class = "form-control" } })
33                 @Html.ValidationMessageFor(model => model.Address, "", new { @class = "text-danger" })
34             </div>
35         </div>
36         <div class="form-group">
37             <div class="col-md-offset-2 col-md-10">
38                 <input type="submit" value="Create" class="btn btn-default" />
39             </div>
40         </div>
41     </div>
42 }

效果:框架

 

 

是的,MVC能夠經過對一些屬性添加必定的特性來對數據進行驗證。這對你們來講可能並不陌生。ui

若是僅僅是這樣就完事了,那麼也就沒事麼意思了。spa

2、經常使用狀況code

在實際的開發中,咱們大都是經過EF,或者其餘方式,使得數據庫中的每個表或視圖,都在代碼中對應的一個類模型,對於經過數據庫生成的模型,咱們不宜修改,退一步講,即便咱們在這個類中對一些屬性增長一些數據驗證的特性,那麼,數據庫發生變化後,若是我再從新生成這些Model,咱們以前添加好的驗證特性將沒有了,那麼,咱們如何解決這樣的問題呢?orm

假如:htm

1     public class UserInfo
2     {    
3         public string UserName { get; set; }
4         public string Sex { get; set; }
5         public string Mobile { get; set; }
6         public string Address { get; set; }
7     }

UserInfo是經過數據庫生成的一個模型,對於數據庫生成的模型,咱們不宜修改。但那是,咱們又須要對這個模型中的某些屬性進行數據驗證,好比須要對UserName屬性進行非空驗證,那麼咱們如何作呢?blog

你們一般會想到部分類,是的,咱們能夠經過部分類來解決上述問題。

首先,咱們將模型中的類加上關鍵字 partial ,而後咱們再寫一個這個模型的部分類。

1     public partial class UserInfo
2     {
3         [Required(ErrorMessage = "UserName不可爲空1111")]
4         public string UserName { get; set; }
5     }

 可是,這樣會提示咱們一個錯誤,就是類中存在重複的屬性,是的,部分類中,屬性是不能夠重名的。那麼,咱們該怎麼辦呢,MVC框架已經給了咱們解決方案了。

咱們能夠這麼寫:

1     [MetadataType(typeof(MeteUserInfo))]
2     public partial class UserInfo
3     {
4         private class  MeteUserInfo
5         {
6             [Required(ErrorMessage = "UserName不可爲空1111")]
7             public string UserName { get; set; }
8         }
9     }

這樣,咱們上述的問題就迎刃而解了。

相關文章
相關標籤/搜索