asp.net MVC model metadata and its customization: preliminary model meta data

Source: Internet
Author: User
Tags definition

After Contronoller is activated, asp.net mvc gets the name of the target action based on the current request context, and then parses the corresponding method and executes it. In the execution of the whole action method, the analysis of model metadata is a very important link. asp.net MVC model actually view model, which represents the final binding to the view of the data, and the model metadata describes the model's data structure, as well as the model of the characteristics of each data member. It is the existence of model metadata that makes the presentation mechanism of templated HTML possible. In addition, model metadata supports the model validation system for ASP.net mvc, because the validation rules for model are defined in model metadata. The model metadata for ASP.net MVC is represented by the type Modelmetadata. Modelmetadata through a series of properties to describe the model and its member-related metadata information, before the formal introduction of these metadata options, we need to first understand the model metadata hierarchy structure.

Model meta-data hierarchical structure

The data type as model can be either a simple string or an object of a value type, or it may be a complex data type. For a complex data type, the metadata based on the type itself and the data member is represented by a modelmetadata, and a data member may be a complex type. So the model metadata represented by the Modelmetadata object actually has a hierarchical structure of trees.

For example, we have a data type that represents a contact with the following definition. Property name, Phoneno, EmailAddress, and address represent names, phone numbers, e-mail addresses, and contact addresses, respectively. The contact address is represented by another data type, Property province, City, district and Street, respectively, representing provinces, cities, urban areas, and streets.

   1:public class Contact
2: {
3: Public string Name {get; set;}
4: Public string Phoneno {get; set;}
5: Public string EmailAddress {get; set;}
6: Public Address address {get; set;}
7:}
8:public class Address
9: {
Public string Province {get; set;}
One: Public, String city, {get; set;}
: Public string District {get; set;}
Public string Street {get; set;}
14:}

If the contact type is used as model, the modelmetadata of its metadata not only has a description of the contact type itself and its attribute members, because its Address property is a complex type, the metadata also needs to describe the 4 attribute members defined in the type. The following figure reflects the hierarchical structure of the model metadata based on the contact type.

The Modelmetadata type that represents the model metadata is used not only to describe a data type as model, but also to recursively describe all its property members (without field members), so the modelmetadata has a tree-structured hierarchy. This can also be seen from the definition of modelmetadata.

   1:public class Modelmetadata
2: {
3: //other Members
4: Public virtual ienumerable<modelmetadata> Properties {get;}
5:}

As shown in the code snippet above, Modelmetadata has a read-only property properties of type Ienumerable<modelmetadata>, representing the Modelmetadata collection that describes the properties/field members. The hierarchical structure of modelmetadata can be represented by UML as shown in the following illustration. Since type Modelmetadata and modelmetadata based on data members are a containment relationship, we can refer to the former as the container (Container) of the latter.

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.