An explanation of data validation for MVC

Source: Internet
Author: User
This article is mainly for you to introduce the MVC data validation of relevant data, with a certain reference value, interested in small partners can refer to

I. GENERAL SITUATION

For people who have used the MVC framework, the data validation of MVC is no stranger, for example, I have a model like this:


public class UserInfo  {    [Required (errormessage = "UserName not nullable 1111")] public    string UserName {get; set;} Public    string Sex {get; set;}    public string Mobile {get; set;}    public string Address {get; set;}  }

Front:


@using (Html.BeginForm ()) {@Html. AntiForgeryToken () <p class= "Form-horizontal" > 

Effect:

Yes, MVC can validate data by adding certain attributes to some properties. This may not be a stranger to everyone.

If it's just that, then it's all right.

Second, the common situation

In the actual development, we mostly through the EF, or other means, so that every table or view in the database, in the code corresponding to a class model, for the model generated by the database, we should not modify, step back, even if we in this class to some of the properties of some data validation features, then, Once the database has changed, if I regenerate the model again, we will not have the validation features we added before, so how do we solve this problem?

If:


public class UserInfo  {public      string UserName {get; set;}    public string Sex {get; set;}    public string Mobile {get; set;}    public string Address {get; set;}  }

UserInfo is a model generated by the database, we should not modify the model generated by the database. But that is, we need to validate some of the properties in this model, such as the need for non-null validation of the Username property, so how do we do it?

We usually think of some classes, yes, we can solve these problems through some classes.

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


public partial class UserInfo  {    [Required (errormessage = "UserName not nullable 1111")] public    string UserName {get; Set }  }

However, this will prompt us with a mistake, that is, there are duplicate properties in the class, yes, in some classes, the attributes are not names. So, what do we do, the MVC framework has given us a solution.

We can write this:


[Metadatatype (typeof (Meteuserinfo))]  public partial class UserInfo  {    Private class Meteuserinfo    {      [Required (errormessage = " UserName non-nullable 1111 ")] public      string UserName {get; set;}    }  

In this way, we can solve the above problems.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.