ASP. NET MVC Model metadata (v) preface
In the previous article we described the various feature classes used for display control above the model, and in this article we will describe the application of these feature classes in detail, although they are not very straightforward with the model metadata, but we can use them to control the display of the runtime during the coding phase.
This is the example model that is used below, the following code 1-2 example is used to obtain model data using a custom models binder, you just need to see the value of the data on the line, for the model binding part of the subsequent length of the introduction.
The Hiddeninputattribute type means to display the applied property as a hidden input field type (Hidden), which contains an attribute Displayvalue indicates whether the hidden input field is displayed, and we modify the content in code 1-1:
@model consoleapplication2.customer@{viewbag.title = "Show";}
Result 1.
Figure 1
650) this.width=650; "src=" Http://images.cnitblog.com/i/627988/201406/252313216117209.png "width=" 642 "height=" 230 "/>
In Figure 1 we see the CustomerID corresponding to the property values “010” displayed in the page as read-only text status, the resulting HTML code is as follows:
<input name= "CustomerID" id= "CustomerID" type= "hidden" value= "010"/>
After you modify the Displayvalue property value in code 1-3 to True, the code in the View section does not change to run the result 2.
Figure 2
650) this.width=650; "src=" Http://images.cnitblog.com/i/627988/201406/252314045332356.png "width=" 657 "height=" 227 "/>
In Figure 2, you have not seen the item represented by the CustomerID property, and see here that we have learned about the use of the Hiddeninputattribute type.
DatatypeattributeLook at Figure 2 above, the value of the Registrationdate property is a datetime format, how do you control the input style of the attribute value? For example, if you want to show the attribute value as a date type, see Code 1-5
Code 1-5
[DataType (datatype.date)]public DateTime registrationdate{get; set;}
The code in the View section does not have to change look at the results Figure 3
Figure 3
650) this.width=650; "src=" Http://images.cnitblog.com/i/627988/201406/252315117527892.png "width=" 646 "height=" 241 "/>
The format style of the output value has been changed.
DisplayAttributeThe DisplayAttribute type is used to indicate the modification of the label value shown by the property, such as name and registrationdate in Figure 3, to show that we have a custom name to use with the DisplayAttribute type, see Code 1-6.
Code 1-6
[Display (name= "name")]public string name {get; set;} [DataType (Datatype.date)] [Display (name= "Registration date")]public DateTime registrationdate{get; set;}
The code for the View section is still the same, so let's look at the results of the run Figure 4.
Figure 4
650) this.width=650; "src=" Http://images.cnitblog.com/i/627988/201406/252316113302692.png "width=" 659 "height=" 227 "/>
From Figure 4 We've seen the change
Model Metadata app (custom view template) -2uihintattributeThis section provides a brief introduction to the use of view templates and Uihintattribute types.
What does the view template mean? is to generate the HTML code that corresponds to the specified view template (including HTML element styles) based on the information provided by the model metadata, so let's look at the example to make it more intuitive.
code 2-1
public class customer { [hiddeninput (Displayvalue=false)] public string CustomerID { get; set; } [display (name= "name")] [ UIHint ("Password")] public string name { Get; set; } [datatype (DataType.Date)] [display (name= "Registration date")] public datetime registrationdate{ get; set; } public address address { get; set; } }
See the Uihintattribute type attribute on the Name property in code 2-1, using it to specify a view template to generate HTML code for this property, so that the password view template is used in order to be visually visible. The meaning of this password view template is that the property value corresponding to the model metadata is born into a single-line text box INPUT element, and the characters in the element are not visible and editable. Take a look at the results Figure 5.
Figure 5
650) this.width=650; "src=" Http://images.cnitblog.com/i/627988/201406/252317425961376.png "width=" 649 "height=" 225 "/>
The corresponding HTML code 2-2
Code 2-2
<input name= "name" class= "text-box single-line password" id= "name" type= "password" value= "Tester"/>
The system provides us with many view templates, but it is also not a mess, to keep the type of view template you want consistent with the specified properties.
Custom View TemplatesA careful friend may find that the value of the address attribute is not found on the display page above, as this is emphasized in the previous article because the type of the address attribute is judged to be a complex type when the model metadata is generated, and the Template view helper does not process the complex type. So how do we show the value in the Address property?
We use custom view templates, of course, more than that, just for the purpose of explaining.
First create a new folder named EditorTemplates under the/views/shared directory in the project, and then create a new strongly typed distribution view file in this folder, type is the type of the specified property. And the named view file is the type name of the specified property, which, as an example, is the address type, which should be the case after completion, figure 6
Figure 6
650) this.width=650; "src=" Http://images.cnitblog.com/i/627988/201406/252318503614311.png "/>
Add this view as in code 2-3.
Code 2-3
<p> @Html. Labelfor (M=>m.addressname) @Html. editorfor (M = m.addressname) </p>
Then modify the Address property in code 2-1, and modify the name of the Addressname property in the address type to be displayed, using the DisplayAttribute type example code we said above 2-4
Code 2-4
public class Customer { [hiddeninput (Displayvalue=false)] public string CustomerID { get; set; } [Display (name= "name")] [uihint ("Password")] public string Name { get; set; } [datatype (datatype.date)] [ Display (name= "Registration date")] public datetime Registrationdate{ get; set; } [uihint ("Address") ] public address address { get; set; } } public class address { [display (name= "address name")] public string addressname { get; set; } }
Then modify the code of our main view page and use the Editfor helper to handle the Address property separately, as shown in code 2-5.
Code 2-5
@model consoleapplication2.customer@{viewbag.title = "Show";}
Then we'll look at the revised results in Figure 7.
Figure 7
650) this.width=650; "src=" Http://images.cnitblog.com/i/627988/201406/252320119864615.png "width=" 656 "height=" 231 "/>
There are some other types of characteristics of the use of the example in the future to modify this article, and now I am not too familiar to write.
Model metadata application (Imetadataaware interface use)-3The previous length of the model metadata generation of the detailed process and the use of a rough explanation, presumably at this time we have some understanding of the model metadata, this section describes how to use the Imetadataaware interface type to implement our direct operation model metadata, To skip those feature classes that use the system's own, they can be set first, but we can also customize the value of the model metadata, so the nonsense is not much to say down to see it.
In the length of the model metadata (ii), the final step in the model metadata generation is to invoke a function in the model metadata generation provider, in which the MVC framework retrieves a collection of all attribute types on the type specified by the current model metadata , the attribute class that implements the Imetadataaware interface is retrieved, and then the current model metadata is manipulated using this implementation type. Maybe my friends are a little vague about this story, so let's look at the sample code.
First we define a type to implement the Imetadataaware interface type, and according to the prerequisite conventions that the MVC framework retrieves, we need to define the custom type as an attribute class, as shown in code 3-1:
Code 3-1
[AttributeUsage (attributetargets.class| Attributetargets.property,allowmultiple=false,inherited=false)] public class mycustommetadataaware:attribute,imetadataaware { public void onmetadatacreated (Modelmetadata metadata) { if (metadata != null) { if (metadata. displayname == "address name") { metadata. displayname = "Address name modified by Imetadataaware"; } } } }
In code 3-1, our custom type Mycustommetadataaware implements the Imetadataaware interface type and modifies the metadata parameter (model metadata) in the onmetadatacreated () method. The Modified property DisplayName is the name of the model metadata controller's corresponding property, and in this mycustommetadataaware type we change the value of this DisplayName property if it matches a certain condition (in order to match the above example).
Let's take a look at our model definition, like code 3-2:
Code 3-2
public class customer { [ Hiddeninput (Displayvalue=false)] public string Customerid { get; set; } [display (Name= "name" )] [uihint ("Password")] public string Name { get; set; } [datatype (datatype.date)] [display (Name= "Registration date")] public datetime registrationdate{ get; set ; } [uihint ("Address")] public Address Address { get; set; } } publIc class address { [display ( Name= "address name")] [mycustommetadataaware] public string AddressName { get; set; } }
In code 3-2, we add the Addressname attribute in the address type of the Address property in the model to our custom attribute class, which is intended to modify the value that this property finally displays on the page.
Let's take a look at the revised results in Figure 8.
Figure 8
650) this.width=650; "src=" Http://images.cnitblog.com/i/627988/201406/252325069869405.png "width=" 654 "height=" 224 "/>
From Figure 8 we can clearly see the corresponding Address bar label name has been modified, for the system provides this Imetadataaware interface we can do more custom operations, this way of programming patterns we are also very common.
Model metadata Part here will be all over, and there will be a lot of details of the part did not speak very in place, not very comprehensive, because Bo Master is small white have what to write bad place friends can put forward, I will learn and then back and forth to modify the length, here for everyone to do a little thin force feel very honored, And thank you all for your support. The next series will be part of the model binding, so please look forward to it.
This article is from the "Jinyuan" blog, be sure to keep this source http://jinyuan.blog.51cto.com/8854733/1430959