Home  >  Article  >  Backend Development  >  Summary of data validation examples in MVC

Summary of data validation examples in MVC

零下一度
零下一度Original
2017-06-24 09:44:141481browse

1. General situation

For those who have used the MVC framework, they will be familiar with MVC data verification. For example, I have a Model As follows:

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     }

Front end:

 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 }

Effect:

##Yes, MVC can validate data by adding certain characteristics to some attributes. This may not be unfamiliar to everyone.

If that’s all it is, then there’s nothing wrong with it.

2. Common situations

In actual development, we mostly use EF or other methods to make every Tables or views correspond to a class model in the code. For the model generated through the database, we should not modify it. To take a step back, even if we add some data verification features to some attributes in this class, then the database will change. Later, if I regenerate these Models, the verification features we added before will be gone. So, how do we solve this problem?

Suppose:

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 is a model generated through the database. We should not modify the model generated by the database. But that is, we need to perform data verification on certain attributes in this model. For example, we need to perform non-null verification on the UserName attribute. So how do we do it?

Everyone usually thinks of partial classification. Yes, we can solve the above problems through partial classification.

First, we add the keyword partial to the class in the model, and then we write a partial class of this model.

1     public partial class UserInfo2     {3         [Required(ErrorMessage = "UserName不可为空1111")]4         public string UserName { get; set; }5     }
However, this will prompt us with an error, that is, there are duplicate attributes in the class. Yes, in some classes, attributes cannot have the same name. So, what should we do? The MVC framework has already given us a solution.

We can write like this:

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     }
In this way, our above problems will be easily solved.

The above is the detailed content of Summary of data validation examples in MVC. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn