ASP. net mvc Model binding summary, mvcmodel
Model binding refers to the process of extracting data from the URL and generating parameters of the corresponding Action method. The preceding Descriptor provides metadata for controllers, behavior methods, and parameters. ValueProvieder obtains data, and the remaining Moder bindings use the above two results to generate a Model. The BindingInfo (type: ParameterBindingInfo) in ParameterDescriptor is an object that implements the IModelBinder interface. The ModelBinder component is used for real Model binding.
ModelBinder component
The following describes the class diagram of the component.
IModelBinder: BindModel method, which implements the Model binding function. Pass in ControllerContext and ModelBindingContext to return the Bound Model.
CustomModelBinderAttribute: The Abstract Feature completes binding between the Model and ModelBinder. The GetBinder method in it is used to obtain ModelBinder.
ModelBinderAttribute: The unique inheritance of CustomModelBinderAttribute, which can be used for parameters, interfaces, enumerations, structures, and class features. However, it is parsed only when it is used as a parameter feature of the Action method during ParameterDescriptor parsing.
IModelBinderProvider: No class in the MVC framework implements this interface and is mainly used for custom extension. The GetBinder method is used to obtain ModelBinder Based on the specified type.
ModelBinderProviderCollection: A set of IModelBinderProvider.
ModelBinderProviders: Has the read-only property BinderProviders, which is of the ModelBinderProviderCollection type.
ModelBinderDictionray: A dictionary set of IModelBinder. The data Type of the Model is the Key, and the corresponding ModelBinder is the Value.
ModelBinders: Read-only attribute with Binders. The type is ModelBinderDictionray. It is only used to obtain ModelBinderDictionray.
Obtain the ModelBinders mechanism: It generally checks whether ModelBinder exists from ParameterDescription. If the ModelBinder does not exist, it uses ModelBinders. the Binders attribute is actually obtained from the internal set _ innerDictionary or _ modelBinderProviders of ModelBinderDictionary. If the two sets cannot be obtained, use the default MolderBinder -- _ defaultBinder in ModelBinderDictionary.
According to the above acquisition mechanism, when you customize the Data Type ModelBinder, different methods are used, and ModelBinder gets different priorities:
1) Add the ModelBinderAttribute or CustomModelBinderAttribute attribute to the parameters in the Action method;
2) the User-Defined ModelBinderProvider does not provide ModelBinder for some Model data and adds it to the set using the ModelBinderProviders. BinderProviders. Add method.
3) Add the ModelBinderAttribute or CustomModelBinderAttribute attribute to the Model type.
4) add ModelBinder through the Binders attribute of ModelBinders.
ModelState
In addition to binding the value to the parameters of the Action method, the Model binding will also be bound to the ViewData of the Controller, which lists the classes related to ModelState.
ModelBindingContext
ModelBindingContext: Has a series of attributes from ParameterDescriptor. Shows how ModelBinderContext is generated and used.
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
Model Verification of aspnet mvc
In this case, do not use feature verification. Use the IValidatableObject interface.
The model implements the IValidatableObject interface, and then implements it in it. For example, I want to get a date comparison.
Public IEnumerable <ValidationResult> Validate (ValidationContext validationContext) {if (this. _ AnnouncementDateEnd <this. _ expiration) {yield return new ValidationResult ("the end date must be later than the start date! ");}}