ASP. net mvc Model metadata (5)

Source: Internet
Author: User
Tags custom name

 

ASP. net mvc Model metadata (5) Preface

In the previous article, we described various feature classes used for display control on the Model. In this article, we will introduce the application of these feature classes in detail, although they are not directly related to the Model metadata, we can use them to control the runtime display in the encoding phase.

 

Model Metadata
  • What is Model?Metadata?
  • Generate ModelMetadata process [1]
  • Generate ModelMetadata process [2]
  • ModelMetaDataDefinition and Explanation
  • ModelMetadata application (common feature application)-1
  • ModelMetadata application (custom view template)-2
  • ModelMetadata application (IMetadataAwareInterface Usage)-3
Model metadata application (common feature application)-1

Example:

Code 1-1

    public class Customer    {        public string CustomerID { get; set; }        public string Name { get; set; }        public DateTime RegistrationDate{ get; set; }        public Address Address { get; set; }     }    public class Address    {        public string AddressName { get; set; }    }

This is the example Model used below. The following code 1-2 example uses the custom Model binder to obtain Model data. You only need to check the data value, the Model binding Section will be further described in the subsequent sections.

Code 1-2

Public object BindModel (ControllerContext controllerContext, ModelBindingContext bindingContext) {return new Customer () {CustomerID = "010", Name = "tester", RegistrationDate = DateTime. now, Address = new Address () {AddressName = "City of the Sky "}};}

 

 

HiddenInputAttribute

The HiddenInputAttribute type indicates that the application property is displayed as a Hidden input field type (Hidden). The property DisplayValue indicates whether the Hidden input field is displayed, let's modify the content in code 1-1:

Code 1-3

[HiddenInput(DisplayValue=true)]public string CustomerID { get; set; }

Then let's take a look at our code on the View Interface:

Code 1-4

@model ConsoleApplication2.Customer@{    ViewBag.Title = "Show";}

Result 1.

Figure 1

In Figure 1, the CustomerID attribute "010" is displayed as the read-only text status on the page. The generated Html code is as follows:

<Input name = "CustomerID" id = "CustomerID" type = "hidden" value = "010"/>

After the DisplayValue attribute value in code 1-3 is modified to true, the code in the view Section remains unchanged. Run result 2.

Figure 2

In Figure 2, the items represented by the CustomerID attribute are no longer visible. I believe we have understood the use of the HiddenInputAttribute type.

 

DataTypeAttribute

As shown in figure 2 above, the value of the RegistrationDate attribute is in the date and time format. How can we control the input style of the attribute value? For example, if you want to display the property value as the date type, see code 1-5.

Code 1-5

[DataType(DataType.Date)]public DateTime RegistrationDate{ get; set; }

The code in the view does not need to be changed. The result is shown in figure 3.

Figure 3

We can see that the format of the output value has changed.

 

DisplayAttribute

The DisplayAttribute type is used to indicate the modification of the label value displayed by the attribute. For example, the Name and RegistrationDate in figure 3, the DisplayAttribute type must be used to display the label value as our custom Name, let's look at 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 of the view remains unchanged. Let's take a look at figure 4 of the running result.

Figure 4

Figure 4 shows the changes

 

Model metadata application (custom view template)-2 UIHintAttribute

This section briefly introduces the use of the view template and the UIHintAttribute type.

What does a view template mean? The Html code (including the Html element style) corresponding to the specified view template is generated based on the information provided by the Model metadata. Let's take a look at the example, which is 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 {get; set ;}}

We can see the features of the UIHintAttribute type from the Name attribute in Code 2-1. It means to specify a view template to generate Html code for this attribute, in this way, the Password View template is used for intuitive display. The meaning of this Password View template is that the attribute values corresponding to the Model metadata are generated as a single-row text box Input element, the characters in the element are invisible and editable. Figure 5 shows the result.

Figure 5

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 they cannot be used in disorder. We need to ensure that the type required for the view template to be used is consistent with the specified attribute.

 

Custom view Template

Careful friends may find that the Address attribute value is not found on the above display page. This is emphasized in the previous article, because the type corresponding to the Address attribute is identified as a complex type when Model metadata is generated, the template view helper does not process the complex type, how can we display the value in the Address attribute?

We use a custom view template to explain more.

First, create a new folder named EditorTemplates under the/Views/Shared directory in the project, and then create a new folderStrongly distributed view filesThe type is the type corresponding to the specified attribute, and the named View File is the type name of the specified attribute. In the example, the Address type name is Address, which should be like this after completion, as shown in Figure 6

Figure 6

Add code 2-3 in this view.

Code 2-3

<p>@Html.LabelFor(m=>m.AddressName)@Html.EditorFor(m => m.AddressName)</p>

Then modify the Address attribute in Code 2-1, and modify the name to be displayed in the AddressName attribute of the Address type. Use the DisplayAttribute sample code 2-4 described above.

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 {get; set ;}} public class Address {[Display (Name = "Address Name")] public string AddressName {get; set ;}}

Then, modify the code on the main view page and use the EditFor helper to process the Address attribute separately, as shown in Code 2-5.

Code 2-5

@model ConsoleApplication2.Customer@{    ViewBag.Title = "Show";}

Then let's take a look at the modified result figure 7.

Figure 7

Some examples of other feature types will be modified later, and I am not familiar with it.

 

Model metadata application (used by the IMetadataAware Interface)-3

The previous sections provide a rough explanation of the detailed process and usage of the Model metadata. At this moment, you may have some knowledge about the Model metadata, this section describes how to use the IMetadataAware interface type to directly operate the Model metadata, so as to skip the feature classes that are provided by the system. They can be set first, however, we can also customize the value of the Model metadata.

 

In the article "Model metadata (2)", the last step after the Model metadata is generated is to call a function in the Model metadata generation provider, in this function, the MVC Framework Retrieves all feature types of the type specified by the current Model metadata and retrieves the feature classes that implement the IMetadataAware interface, use this implementation type to operate the current Model metadata. Maybe my friends may have a vague description. Let's look at the sample code.

First, we define a type to implement the IMetadataAware interface type. According to the MVC Framework's prerequisite, we need to define the custom type as a feature class, as shown in code 3-1:

Code 3-1

[AttributeUsage (AttributeTargets. Class | AttributeTargets. Property, AllowMultiple = false, Inherited = false)] public class metadata: Attribute, metadata {public void OnMetadataCreated (ModelMetadata metadata) {if! = Null) {if (metadata. DisplayName = "address name") {metadata. DisplayName = "address name modified by IMetadataAware ";}}}}

In code 3-1, the custom type MyCustomMetadataAware implements the IMetadataAware interface type and modifies the metadata parameter (Model metadata) in the OnMetadataCreated () method, the modified DisplayName attribute is the name displayed for the attribute corresponding to the Model metadata controller. In this MyCustomMetadataAware type, we modify this DisplayName attribute value if it meets certain conditions (to match the preceding example ).

Let's take a look at our Model definition, such as 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 {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 corresponding to the Address attribute in the Model to our custom feature class, it is intended to modify the value that is finally displayed on the page.

The modified result is shown in figure 8.

Figure 8

As shown in figure 8, we can clearly see that the tag name in the corresponding address bar has been modified. For the IMetadataAware interface provided by the system, we can perform more custom operations, this programming mode is also very common.

 

The metadata section of the Model is all over here, and many details are not fully discussed, it is a great honor for everyone to learn and modify the length of the article here because the bloggers are friends who may not write well, thank you for your support. The next series will be part of the Model binding.

 

 

 

 

Author: Jin Yuan

Source: http://www.cnblogs.com/jin-yuan/

The copyright of this article is shared by the author and the blog Park. You are welcome to reprint this article. However, you must keep this statement without the author's consent and go to the Article Page.

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.