首頁  >  文章  >  後端開發  >  總結MVC中資料驗證實例

總結MVC中資料驗證實例

零下一度
零下一度原創
2017-06-24 09:44:141428瀏覽

一、一般情況

對於使用過MVC框架的人來說,對MVC的資料驗證不會陌生,例如,我有一個Model如下:

1     public class UserInfo2     {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可以透過對一些屬性添加一定的特性來驗證資料。這對大家來說可能並不陌生。

如果只是這樣就完事了,那麼也就沒事麼意思了。

二、常用情況

在實際的開發中,我們大都是透過EF,或是其他方式,使得資料庫中的每一個表或視圖,都在程式碼中對應的一個類別模型,對於透過資料庫產生的模型,我們不宜修改,退一步講,即使我們在這個類別中對一些屬性增加一些資料驗證的特性,那麼,資料庫發生變化後,如果我再重新產生這些Model,我們之前加入好的驗證特性將沒有了,那麼,我們該如何解決這樣的問題呢?

假如:

1     public class UserInfo2     {    
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屬性進行非空驗證,那我們該怎麼做呢?

大家通常會想到部分類,是的,我們可以透過部分類來解決上述問題。

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

1     public partial class UserInfo2     {3         [Required(ErrorMessage = "UserName不可为空1111")]4         public string UserName { get; set; }5     }
 但是,這樣會提示我們一個錯誤,就是類別中存在重複的屬性,是的,在部分類別中,屬性是不可以重新命名的。那麼,我們該怎麼辦呢,MVC框架已經給了我們解決方案了。

我們可以這麼寫:

1     [MetadataType(typeof(MeteUserInfo))]2     public partial class UserInfo3     {4         private class  MeteUserInfo5         {6             [Required(ErrorMessage = "UserName不可为空1111")]7             public string UserName { get; set; }8         }9     }
這樣,我們上述的問題就迎刃而解了。

以上是總結MVC中資料驗證實例的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn