ASP. net mvc Model metadata (1)

Source: Internet
Author: User

 

ASP. net mvc Model metadata (1) Preface

When I was a beginner, I was vague about the concept of Model metadata, or I didn't have a Model in my brain, as a tom, I still couldn't understand some articles on the Internet. Then I tried to back down. I was about to skip this part to learn other articles, in this process, I did not have the spirit to do anything. Just like a thorn in my heart, I finally turned back. Now I will share with you what Model metadata is, I hope that it will be helpful to some friends and give me a rough idea of what Model metadata is.

 

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

 

What is Model metadata?

We have been holding on for so long, so we can directly cut into the topic without talking nonsense.

Since it is called Model metadata, you must first have a Model (Model refers to the view Model ). Let's define the Model first. See code 1-1:

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 Name { get; set; }    }

A simple example Model is defined to show it.

The example is followed by the example in the previous section, and friends who haven't seen the sentence will be okay. Find the default controller in your project and add the code 1-2 to it.

Code 1-2

1         public ViewResult Show(Customer customer)2         {3             return View(customer);4         }

Right-click the Show () method, add a view, and select create strong view, as shown in Figure 1-1.

Figure 1-1

(If you are afraid of coding mistakes, you can select the model type you want to define in the drop-down box, the type added in the VS environment may not be displayed yet. Click Cancel to regenerate the following project, and then follow the steps above to find the type you just defined)

Then, add code 1-3 to the view.

Code 1-3

<p>@Html.EditorForModel()</p>

At this time, debugging is not available. We need to create a new folder in the project, name it Binders, and name the new class file MyCustomBinderModel under this folder. cs, open and modify it to code 1-4.

Code 1-4

Using System. web. mvc; using ConsoleApplication2; namespace MvcApplication. binders {public class MyCustomBinderModel: IModelBinder {public object BindModel (ControllerContext controllerContext, ModelBindingContext bindingContext) {return new Customer () {CustomerID = "010", Name = "tester ", registrationDate = DateTime. now, Address = new Address () {Name = "City of the Sky "}};}}}

(The namespace ConsoleApplication2 referenced in code 1-4 is the part of the Model. It should be defined in the Models folder of the Project. Do not learn this method here .)

Code 1-4 is a self-defined model binder. It is required to configure our debugging environment. After being defined, you also need to add the custom model binder to the system. add code 1-5 to the Application_Start () method under the MvcApplication type in the asax file.

Code 1-5

ModelBinders.Binders.Add(typeof(Customer), new Binders.MyCustomBinderModel());

At this time, let's debug it. Result 1-2.

Figure 1-2

When I saw this, some friends would ask why I didn't talk about Model metadata at all? Please do not worry.

Figure 1-3

I still want to modify the view model and add some control display features, but to make it concise and clear, I will leave the most basic impression and decide to discard it. Of course, I will discuss it later.

Now, let's talk about the @ Html in the view. editorForModel () code, and a series of methods are view helper methods. Why is this name used to help us generate the HTML code required by the view, so we thought that none of these view guides can help us generate HTML code? By the way, according to the Model metadata, we can now think of the Model metadata object as encapsulating various display information in the Customer type. Careful friends may find something missing in Figure 1-2, the Customer's Address attribute is not displayed. Why? These problems are explained in the subsequent chapters in the process of generating Model metadata and its object structure.

The previous article may have paved the way for so many pictures to illustrate what the Model metadata is. For Beginners, they have not considered the feelings of the great gods, because I know what I want to know most when I first came into contact with this part of knowledge, I hope you will not spray it. With this foundation, we will be very helpful in understanding the subsequent articles and will be able to impress everyone in the brain.

 

 

 

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.