Summary of ASP. net mvc ModelValidator, mvcmodelvalidator

Source: Internet
Author: User

Summary of ASP. net mvc ModelValidator, mvcmodelvalidator

When a user enters data through the UI to interact with the program, a potential error may occur, data error. to check whether the data submitted by the user is correct, data verification is required. In ASP. in. net mvc, every time before an Action is executed, the Model of the input Action is verified in a variety of formats. These verifications are executed through the validation component ModelValidator in MVC, different ModelValidator tasks have different verification tasks. Different ModelValidator tasks are executed based on the encoding staff settings. Different ModelValidator tasks are provided by the corresponding ModelValidator, the following is the overall class diagram.

ModelValidator: Abstract class, GetClientValidateRule, used to obtain client verification rules. The IsRequire attribute indicates that the member must perform verification. The default value is false. Validate indicates that the ModelValidatorResult set is returned, if it succeeds, Null, empty set, or ValitionResult is returned.

ModelValidatorResult: stores the result of member verification. The MemberName attribute stores the member name, and the Message attribute stores error information. If the object to be verified is the Model itself, the MemberName attribute is an empty string, the instance of this class is returned when verification fails.

ValidationResult: The instance of this class is returned only when the verification is successful in some cases, with a static read-only member Success.

DataAnnotationsModelValidator: Inherit from ModelValidator for feature-based verification, such as RequiredAttribute.

ClientModelValidator: Inherits ModelValidator and is only used for client verification. The Validate method is the server-side verification method. Therefore, the empty set is always returned here. GetClientValidateRule returns the client verification rules.

NumericModelValidator&DateModelValidator: Inherit from ClientModelValidator, which is used for number and date verification. The error messages for verification are obtained in the resource file maintained internally, which makes it impossible to customize messages.

IDataErrorInfo: The member Error is its own Error message. The indexer is used to obtain member attributes.

DataErrorClassModelValidator&DataErrorPropertyModelValidator: Inherits ModelValidator, which is an internal type. It processes instances of classes that implement IDataErrorInfo. The former is for classes, and the latter is for members (especially for attributes, the logic of the deduction and verification is defined in the class that implements IDataErrorInfo. the Validator verification class only calls the Model attribute directly to obtain the verification result.

IValidatableObject: This interface represents another Authentication mode. It is the data object's own authentication. The verification context is passed in and the ValidationResult is returned.

ValidatableAdapter: Inherits ModelValidator, which is used to verify the class that implements IValidaableObject.

ModelValidatorProvider: Abstract class. The accumulation of all providers is used to provide ModelValidator. The GetValidator method returns the set of ModelValidator Based on the ModelMetadata metadata and the context of the ControllerContext controller.

AssocatedValidatorProvider: Inherit from ModelValidatorProvider. It mainly constructs ModelValidator from the Attribute extracted by ModelMetadata. GetTypeDescriptor obtains the description object based on the given Type. Mechanism: when verifying the Attribute of a class, based on GetTypeDescriptor, get the description class Descriptor, get the feature from Descriptor, and finally get ModelValidator from the feature. This Validator is the Validator to be provided by AssocatedValidatorProvider.

DataAnnotationsModelValidatorProvider: Inherits AssocatedValidatorProvider and provides DataAnnotationsModelValidator. Mechanism: Inherit the GetValidator method from AssocatedValidatorProvider, and select the feature that integrates ValidationAttribute from the input ModelMetadata to create DataAnnotationsModelValidator.

ClientDataTypeModelValidatorProvider: Inherits ModelValidatorProvider and provides NumericModelValidator and DateModelValidator. It identifies numbers (all byte, int, float, decimal, etc.) and dates based on ModelMetadata.

DataErrorInfoModelValidatorProvider: Inherit from ModelValidatorProvider, provide validator and DataErrorPropertyModelValidator, and provide a mechanism: If the verified object implements the IDataError interface, DataErrorClassModelValidator is returned in the returned collection; the verified object is the container property, and dataerrorpropertymodel, in short, one class implements the IDataError interface and returns DataErrorClassModelValidator and DataErrorPropertyModelValidator of the verified attribute.

ModelValidatorProviderCollection: Set of ModelValidatorProvider to manage each Provider. When constructing this class, add the preceding three providers. providers in the set can be added and deleted, as can custom providers.

ModelValidatorProviders: Use a static property Providers to reference the above ModelValidatorProviderCollection.

CompositeModelValidator: Inherit from ModelValidator, which is a private class called during verification. It automatically identifies various authentication methods internally and calls different modelvalidatorproviders to generate ModelValidator, generally, the verification order is to first verify the members of the class, then verify the class itself, bottom-up.


How does aspnet mvc configure multiple models in one View?

However, this example is implemented using PartialView. This example uses ViewModels to compile the program.
In the past experience, I learned the advantages of the traditional model. Therefore, in the development process, the traditional model should be used, the official website examples of MVC do not directly show that two models are used in one View. Therefore, many of the examples are imported into ASP for the first time. net mvc friends will be stuck here. Basically, we can use ViewData to compile images. However, as mentioned above, scatter is not the first test in development, therefore, it is a good way to package the objects to be renewed.
In the example, a new item named ViewModels is created and a new Class content is added as follows:
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Web;
Namespace MvcApplication. ViewModels {public class demoshopViewModels {public IEnumerable <shop {get; set ;}
Public IEnumerable <demo {get; set ;}}}
Use this ViewModels to package two different models.
The usage in the View is no different.
<% @ Page Language = C # MasterPageFile = ~ /Views/Shared/Site. Master Inherits = System. Web. Mvc. ViewPage <MvcApplication. ViewModels. demoshopViewModels %
<Asp: Content ID = aboutContent ContentPlaceHolderID = MainContent runat = server
<% Foreach (var item in Model. shop) {// shop Models} %
<% Foreach (var item in Model. demo) {// here is demo Models }%</asp: Content
After reading the above Code, we should not find that it is okay to use multiple Models. However, during the development process, you may not want to use the dynamic model to compile the program (someone else wants to enable VideModels). Therefore, the following describes how to use ViewData.
ViewData is the required information in the Controller.
Public ActionResult About (){
ViewData [shop] = store;
ViewData [demo] = demo;
// The preceding two examples show how to call data.
Return View ();}
Because it cannot be directly committed, some changes may occur in View usage.
<% @ Page Language = C # MasterPageFile = ~ /Views/Shared/Site. Master Inherits = System. Web. Mvc. ViewPage %
<Asp: Content ID = aboutContent ContentPlaceHolderID = MainContent runat = server
<% Var shop = ViewData [... the remaining full text>

How does one view in aspnet mvc correspond to multiple models?

It can correspond to multiple models. You need to create one more model. Now you have two models, DeviceInfoModel and UploadImageModel. You need to return them to each view. You can create one more class. For example:
Public class model2
{
Public List <DeviceInfoModel> list {get; set ;}

Public DeviceInfoModel list {get; set ;}
}
You can assign a value to this model in Controllers and return the result.
In this way, you will return model2 in the view you access, and then you will be able to access the model you need during the view. In this way, no matter how many models are needed on your page

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.