The model binding process designed to perform binding input parameters for the target action method is accompanied by validation of model. With the corresponding validation features, we can define validation rules directly on the model type declaratively, as part of the model metadata. In the model binding process, Modelbinder validates the validation rules that are defined on the model metadata based on the property, after the Valueprovider provides the corresponding property value for a property of the model object. The whole model validation system of ASP.net mvc is centered on the component Modelvalidator, or the validation of model objects is finally accomplished by a Modelvalidator object. So it is necessary for us to first recognize the modelvalidator and the mechanism behind it.
First, Modelvalidator
In the ASP.net MVC application Programming interface, all modelvalidator inherit from the abstract type Modelvalidator directly or indirectly. As shown in the following code snippet, Modelvalidator has a boolean-type read-only property isrequired that indicates whether the modelvalidator is the necessary validation of the target data and returns false by default. Getclientvalidationrules returns a collection with an element type of modelclientvalidationrule. Modelclientvalidationrule is an encapsulation of the client validation rules, which we will describe in detail when we do client-side validation.
1:public Abstract class Modelvalidator
2: {
3: //other Members
4: Public virtual ienumerable<modelclientvalidationrule> getclientvalidationrules ();
5: Public abstract ienumerable<modelvalidationresult> Validate (Object container);
6:
7: Public virtual bool isrequired {get;}
8:}
The real validation of the target data is done by calling the Validate method, and the input parameter of the method container the formally validated object. The validate returns a collection that represents the validation result with an element type of modelvalidationresult, as shown in the following definition.
1:public class Modelvalidationresult
2: {
3: Public modelvalidationresult ();
4:
5: Public string MemberName {get; set;}
6: Public string, {get; set;}
7:}
Modelvalidationresult has two string-type property membername and messages that represent the name of the validated data member, which represents an error message. In general, when they are used to validate a complex type object, the MemberName property for the Modelvalidationresult object returned for the type itself is an empty string, and for property validation, The property name is used directly as the MemberName property value.
The Modelclientvalidationrule collection is returned only if the validation fails. If the target data conforms to all validation rules, the Validate method returns null or an empty Modelvalidationresult collection directly. It is worth mentioning that when we call the Modelvalidator validate method to determine whether the target data is validated, the method return value is sometimes compared to the static read-only field success defined in type Validationresult with the following definition. In fact, the value of the Success field that represents validation success is null.
1:public class Validationresult
2: {
3: //other Members
4: Public static readonly Validationresult Success;
5:}
Second, Dataannotationsmodelvalidator
Readers who have a little knowledge of asp.net mvc should know that we can apply the corresponding validation annotation attributes (such as RequiredAttribute, Rangeattribute and Regularexpressionattribute, etc.) to define the corresponding validation rules, which is the default model validation method provided by ASP.net MVC. The corresponding Modelvalidator type based on the data Annotation (Annotation) feature is Dataannotationsmodelvalidator, which we will introduce separately in subsequent articles.
Third, Clientmodelvalidator
Clientmodelvalidator is the internal type defined in the assembly System.Web.Mvc.dll that is used for validation of data types on the client. As shown in the following code fragment, in addition to specifying the model metadata and the controller context in the constructor, you need to specify the authentication type (data type) and error message as a string.
1:internal class Clientmodelvalidator:modelvalidator
2: {
3: Public clientmodelvalidator (modelmetadata metadata, ControllerContext controllercontext, string Validationtype,string errormessage);
4: Public sealed override ienumerable<modelclientvalidationrule> Getclientvalidationrules ();
5: Public sealed override ienumerable<modelvalidationresult> Validate (Object container);
6:}
Because Clientmodelvalidator is limited to client-side validation, its Validate method (server-side validation) always returns an empty Modelvalidationresult collection. The Getclientvalidationrules method generates the corresponding client-side validation rules based on the specified validation type and error message.
The Clientmodelvalidator has two inheritors, which are numericmodelvalidator and datemodelvalidator for client-side validation of numeric types and date types respectively. As shown in the following code snippet, these two clientmodelvalidator are used to indicate that the string of validation data types is "number" and "date", respectively. The string that represents the error message is obtained from the internally maintained resource file. This actually poses a problem that we cannot customize the error message.
1:internal Sealed class Numericmodelvalidator:clientmodelvalidator
2: {
3: Public numericmodelvalidator (modelmetadata metadata, ControllerContext controllercontext)
4: : Base (metadata, ControllerContext, "number", Clientdatatypemodelvalidatorprovider.getfieldmustbenumericresource (ControllerContext))
5: {
6: }
7:}
8:
9:internal Sealed class Datemodelvalidator:clientmodelvalidator
10: {
One: Public datemodelvalidator (modelmetadata metadata, ControllerContext controllercontext)
: Base (metadata, ControllerContext, "date", Clientdatatypemodelvalidatorprovider.getfieldmustbedateresource (ControllerContext))
: {
: }
15:}