ASP. NET MVC Model metadata (i)

Source: Internet
Author: User

ASP. NET MVC model metadata (I.) preface

When I was a beginner, the concept of model metadata was vague, or in the brain does not have a model of it, as a small white I go to see some articles on the Internet or two eyes a black what can not understand, and then I want to retreat, I flinched to skip this part to learn the other, in the process do not have the spirit, Just like there is a thorn in the heart, in the end I still back, and now to share with you what is the model metadata, hope to see some friends have a little help, probably understand what the model metadata is exactly what the thing.

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

What is model metadata?

The delay has been so long, no nonsense we cut into the subject directly.

Since the model metadata is called the model, it has to be modeled first (model refers to the view models). Let's define the model first, see Code 1-1:

Code 1-1

     Public classCustomer { Public stringCustomerID {Get;Set; }  Public stringName {Get;Set; }  PublicDateTime registrationdate{Get;Set; }  PublicAddress Address {Get;Set; } }     Public classAddress { Public stringName {Get;Set; } }

A very simple example model that defines what we are going to show it.

The example is followed by an example of the previous space, or a friend who has not seen it in that sentence. Find the default controller in your own project and add code 1-2 to it.

Code 1-2

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

Then we right-click the show () method, add the view, and tick create strongly typed view, shown in 1-1.

Figure 1-1

(here to tell you, if you are afraid of a mistake in the hand code can be in the drop-down box to select the model type you want to define to the view, you may have just added a type in the VS environment has not been shown, then click Cancel, rebuild the following items, and then follow the above steps to find the type you just defined)

Then "Add" and add code 1-3 to the view.

Code 1-3

< P >@Html. Editorformodel ()</p>

This time can not be debugged, we need to create a new folder in the project, named Binders, and then the new class file under this folder named MyCustomBinderModel.cs, opened to modify as code 1-4.

Code 1-4

usingSYSTEM.WEB.MVC;usingConsoleApplication2;namespacemvcapplication.binders{ Public classMycustombindermodel:imodelbinder { Public ObjectBindmodel (ControllerContext controllercontext, Modelbindingcontext bindingcontext) {return NewCustomer () {CustomerID="010", Name="Test Personnel", Registrationdate=DateTime.Now, Address=NewAddress () {Name="the city of the Sky"                }            }; }    }}

(The namespace referenced in code 1-4, ConsoleApplication2, is part of the model and should be defined in the project's Models folder, so let's be careful not to learn this way.) )

Code 1-4 is our own definition of the model binder, the configuration of our debugging environment required, after the definition, we also need to add a custom model binder to the system, you need the mvcapplication type in the Global.asax file Application_ Add code 1-5 to the Start () method.

Code 1-5

ModelBinders.Binders.Add (typeofnew Binders.mycustombindermodel ());

This time we will debug, result 1-2.

Figure 1-2

See here, some friends will ask to say so much has not talked about the model meta-data ah? Don't be impatient, don't worry, look at 1-3.

Figure 1-3

I would also like to modify the view model and some of the features of the control display, but for the sake of brevity to leave the most basic impression decision or discard, of course, the following will be mentioned.

Now we are talking backwards, there is a sentence in the view @Html. Editorformodel () code, and a series of methods are the view helper method, why called this name, because it helps us to generate the view of the required Html code, So do we think that there are no such view helper methods that are based on helping us generate HTML code? Yes, according to the model metadata, now you can think of the model metadata object as encapsulated in the customer type of various display information, careful friends may find that figure 1-2 is missing something, customer's address attribute is not displayed, why? These problems and the generation of the model metadata and its object structure are explained in subsequent chapters.

Perhaps the previous article foreshadowing so much behind is a diagram to illustrate what is the model metadata a bit of a pit, for beginners do not consider the feelings of the great gods, because I know I just touched this part of the knowledge of the most want to know what, so look at the gods do not spray. With this foundation to follow the understanding of the article is very helpful, but also let everyone in the brain to leave an impression.

Jinyuan

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

This article is copyrighted by the author and the blog Park, welcome reprint, but without the consent of the author must retain this statement, and on the article page

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.