ASP. NET MVC model metadata (IV)

Source: Internet
Author: User

ASP. NET MVC model metadata (IV) Preface

The previous space explains the model metadata generation process, and does not have a detailed explanation of the internal and model metadata data structures of the model metadata generation process. After reading this article will have a clearer understanding of the model metadata, of course, it will not be particularly comprehensive, because there is space behind. Hope to bring good results for everyone.

Model Meta Data
    • What is model meta data?

    • Generate Model meta-data process "one"

    • Generate Model meta-data process "two"

    • Modelmetadata the definition, explanation

    • Model Meta Data Application (common feature application)-1

    • Model Metadata app (custom view template)-2

    • Model Meta Data Application ( Imetadataaware interface Use) -3

Definition and explanation of Modelmetadata

Can we define the model metadata generation ourselves? The answer is yes, it must be. The MVC framework provides us with a top-level base class that, when invoked, gets the system default implementation class (or our custom implementation Class) from the current context. Let's take a look at example code 1-1.

Code 1-1

 public class mycustommodelmetadataprovider:d ataannotationsmodelmetadataprovider    {         Protected override modelmetadata createmetadata (Ienumerable<attribute> attributes,  Type containerType, Func<object> modelAccessor, Type modelType,  String propertyname)         {             DataAnnotationsModelMetadata result = new  Dataannotationsmodelmetadata (This, containertype, modelaccessor, modeltype, propertyname,  displaycolumnattribute);             return  result;        }    } 

The Mycustommodelmetadataprovider type in code 1-1 inherits from the Dataannotationsmodelmetadataprovider type, and overrides the method of Createmetadata (). The Createmetadata () method assigns values to the various properties of the model metadata based on the attribute information in the parameter attributes. This will be said below, the attributes parameter is not parsed in code 1-1, but only a model metadata type (Dataannotationsmodelmetadata inherited from Modelmetadata) is instantiated for return. After this is defined, the system does not invoke our custom implementation, but it needs to be added to the system context when the project is started, and we are Application_Start () in the Mvcapplication type in the Global.asax file. method to add the sample code 1-2.

Code 1-2

Modelmetadataproviders.current = new Mycustommodelmetadataprovider ();

After this definition, the system framework will invoke our custom implementation at the time of execution, and we can use the example of the previous space to run directly, what results I have not tried but certainly there is no special effect, the real purpose is not here, but in Createmetadata () A breakpoint is set at the entrance of the method (Figure 1) and then we press F5 again to execute the program, and the program executes the Createmetadata () method of our custom implementation.

Figure 1

650) this.width=650; "src=" Http://images.cnitblog.com/i/627988/201406/242329092992414.png "width=" 738 "height=" 69 " Border= "0" hspace= "0" vspace= "0" title= "" style= "width:738px;height:69px;"/>

What do you mean by that? The significance of this is that we can open the Debugging Instant window every time the breakpoint comes in, and enter the Createmetadata () method parameter Modeltype to see the type or attribute of the model metadata that is currently being generated, and to learn more in depth. There is another meaning that proves the process of generating as I said in the previous article.

Let's take a look at how the model metadata is manipulated by default in the Dataannotationsmodelmetadataprovider type provided by the system, and see the default implementation code first.

Code 1-3

Protected override modelmetadata createmetadata (Ienumerable<attribute> attributes,  Type containerType, Func<object> modelAccessor, Type modelType,  String propertyname)         {             List<Attribute> attributeList = new List< attribute> (attributes);             Displaycolumnattribute displaycolumnattribute = attributelist.oftype<displaycolumnattribute > (). FirstOrDefault ();             Dataannotationsmodelmetadata result = new dataannotationsmodelmetadata (this,  Containertype, modelaccessor, modeltype, propertyname, displaycolumnattribute);                         // do [hiddeninput]  before [UIHint], so you can override the template hint             HiddenInputAttribute  Hiddeninputattribute = attributelist.oftype

In code 1-3, we see that first the AttributeList variable is converted to the attribute collection type based on the parameter attributes, and then the attribute of the first displaycolumnattribute type is searched in this collection. Let's not say what this feature type is for the time being, because I don't quite understand it now.

Then you instantiate a dataannotationsmodelmetadata type of metadata based on the parameters in the Createmetadata () method, as mentioned above. Keep looking down, and then it's time to get the attribute instance of the first Hiddeninputattribute type from the attributelist variable, after judging not to be empty, Assign a value to the two properties of the model metadata dataannotationsmodelmetadata type variable result (the model metadata dataannotationsmodelmetadata type variable result is collectively called result) First, the first is the Templatehint property of the model metadata, which represents which view template is used by the object represented by the model metadata to generate HTML code (the contents of the view template are explained later in this series, and then look back, Learning to feel is an iterative process). Then is the assignment of the Hidesurroundinghtml property, corresponding to the Displayvalue value of the Hiddeninputattribute type, The Hiddeninputattribute type represents whether the attribute or field value is displayed as a hidden INPUT element, and if we write [Hiddeninput (Displayvalue = False)], The Hidesurroundinghtml property value is true, meaning that the object model is rendered using the associated HTML element, meaning that the property or field we specify is rendered with the hidden input field associated with the Hiddeninputattribute type. This may be a bit around, but without prejudice, the next space will tell you the effect of the example.

To go back to the topic, the following is a collection of uihintattribute types obtained from AttributeList, and after a judgement gets an instance of the Uihintattribute type, and is still assigned to the Templatehint attribute (said above) , this is covered, and we need to pay attention to this when we use the default model metadata provider, and then continue looking down.

Gets an instance of the first Editableattribute type from AttributeList, and sets the IsReadOnly property value of result based on the value of the AllowEdit attribute in the Editableattribute type instance, Indicates whether the model is read-only, the Editableattribute type indicates whether the model is editable and the following ReadOnlyAttribute type is similar, except that the same is true for the read-only effect. The value of the property set in the two type is reversed.

The same attributelist gets the first instance of the Datatypeattribute type that conforms to the type condition, and one instance of the Displayformatattribute type, where another function is called in the default provider. This is not a long introduction, I would like to say a little bit on the line. Why put these two together? Because they are all related to the settings of the specified model output format.

The Scaffoldcolumnattribute type instance indicates whether to use scaffolding (one of the template view aids, Editorformodel belongs to one), and when the attribute class is used on a property, the property is skipped directly when using the scaffolding. This property is also not found in the generated page. (have been rejected)

Instances of the same DisplayAttribute type are also obtained from AttributeList, the first of which conforms to the type condition, and a name attribute in the DisplayAttribute type instance is set to the DisplayName property of the result. This property means that the specified model displays the value of the page. The Displaynameattribute type instance has a similar meaning to the DisplayAttribute type, except that the Displaynameattribute type can be used for class types, and the definition is known when we look at it.

Finally, the meaning of the RequiredAttribute type instance is explained in the model validation space.

This article is from the "Jinyuan" blog, please be sure to keep this source http://jinyuan.blog.51cto.com/8854733/1430532

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.