Model verification system how to achieve the operating mechanism?

Source: Internet
Author: User

In the previous three articles ("Modelvalidator", "Modelvalidatorprovider" and "Modelvalidatorproviders") In detail, we describe the modelvalidator and related delivery mechanisms that are really used for model validation, and then we'll talk about the Modelvalidator-core model validation system, How the validation of the data objects obtained through model binding is achieved.

First, talk about from Modelstate

We know that the ViewData of the Controller object contains a collection of element types modelstate to represent the state of the model. Validation results that provide data are saved in addition to the data stored in the model binding process through the valueprovider body.

 1: [Serializable] 
2:public class Modelstate
3: {
4:public modelerrorcollection Errors {g Et
5:public valueproviderresult Value {get; set;}
6:}
7:
8: [Serializable]
9:public class Modelerrorcollection:collection<modelerror>
: {
11:public modelerrorcollection ();
12:public void Add (Exception Exception);
13:public void Add (String errormessage);
14:}
:
[Serializable]
17:public class Modelerror
: {
19:public modelerror (Exception exc Eption);
20:public modelerror (String errormessage);
21:public modelerror (Exception Exception, String errormessage);
:
23:public string errormessage {get;}
24:public Exception Exception {get;}
:}

As shown in the code snippet above, Modelstate has the value and errors two core properties, which represent the Valueproviderresult object provided by the Valueprovider, which represents the collection of errors for that data object. Its type is modelerrorcollection. Modelerrorcollection is a collection of element types Modelerror, and a Modelerror object describes errors through error messages and exceptions.

Example Demo: Verify the setup of Modelerror during model binding

Model validation can be viewed as part of the model binding process, which validates the data provided in the process of generating a target action method parameter value, In the case of validation failure, the validation results are written in Modelerror to the modelstate of the current controller ViewData, and now we confirm this by a simple example. We still use the contact as model type many times, as shown in the following code fragment, the type contact and address and all their attributes apply the validation attribute defined above Alwaysfailsattribute (asp.net MVC to Modelvalidator as the core model validation system: Modelvalidatorproviders "), and set the appropriate error message.

1: [Alwaysfails (errormessage = "Contact")]
2:public class Contact
3: {
4: [Alwaysfails (errormessage = "Contact.name")]
5:public string Name {get; set;}
6:
7: [Alwaysfails (errormessage = "Contact.phoneno")]
8:public string Phoneno {get; set;}
9:
[Alwaysfails (errormessage = "contact.emailaddress")]
11:public string EmailAddress {get; set;}
12:
[Alwaysfails (errormessage = "contact.address")]
14:public address address {get; set;}
15:}
16:
[Alwaysfails (errormessage = "Address")]
18:public class Address
19: {
[Alwaysfails (errormessage = "address.province")]
21:public string Province {get; set;}
22:
[Alwaysfails (errormessage = "address.city")]
24:public string City {get; set;}
25:
Num: [alwaysfails (errormessage = "address.district")]
27:public string District {get; set;}
28:
: [Alwaysfails (errormessage = "Address.street")]
30:public String Street {get; set;}
31:}

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.