ASP. NET MVC Model binding (i)

Source: Internet
Author: User

ASP. NET MVC Model binding (i) preface

The end of the Modelmetadata series, from the beginning of this chapter into the Model binding section, this series after reading you will have a clearer understanding of the model binding, this article for the model binder of the most basic application of a simple example show, The goal is to give you a better understanding of what the model binder is for subsequent space.

Model binding
    • Imodelbinder, custom model binder Simple implementation
    • Location of the model binder in the MVC framework
    • The default model binder generation process in MVC
    • Simple application of Imodelbinderprovider
    • Ivalueprovider the location and process generated in the MVC framework
    • Ivalueprovider's application Scenario
    • The realization of Ivalueprovider Namevaluecollectionvalueprovider
Imodelbinder, custom model binder Simple implementation

Model Binder in the previous space example also involves, in this article to re-talk about, see the previous space of friends can probably browse this article, and then skip to the next article.

For the model binder system, which provides a default binder Defaultmodelbinder type, and it implements the Imodelbinder interface, let's take a look at the definition of the Imodelbinder interface, code 1-1.

Code 1-1

 Public InterfaceImodelbinder {//Summary://binds the model to a value using the specified controller context and binding context. //        //Parameters://ControllerContext://The controller context. //        //BindingContext://The binding context. //        //return Result://The binding value.         ObjectBindmodel (ControllerContext controllercontext, Modelbindingcontext BindingContext); }

See Code 1-1, a Bindmodel () method is defined in the Imodelbinder interface, and there are two parameters that are learned by the system-provided annotations that one is a controller context object and one is a binder context object. The controller context object means that all the underlying information is contained within the scope of the current controller's execution, and so is the binding context. The following pages will give a detailed introduction to this series of contextual objects, which has been brought here.

Now let's implement the Imodelbinder interface to define its own model binder, of course, you can also inherit from the Defaultmodelbinder type rewrite the Bindmodel () method. Let's take a look at our custom implementation, code 1-2.

Code 1-2

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

The reference to the ConsoleApplication2 namespace is because ViewModel is defined there, which is the type to be returned by the Bindmodel () method in code 1-2, In code 1-2 we simply instantiate a ViewModel (customer type), and we can actually do a lot of things. Let's look at the definition of ViewModel, code 1-3.

Code 1-3

 Public classCustomer {[Hiddeninput (Displayvalue=false)]         Public stringCustomerID {Get;Set; } [Display (Name="name")] [UIHint ("Password")]         Public stringName {Get;Set; }        [DataType (Datatype.date)] [Display (Name="Registration Date")]         PublicDateTime registrationdate{Get;Set; } [UIHint ("Address")]         PublicAddress Address {Get;Set; } }     Public classAddress {[Display (Name="Address name")] [Mycustommetadataaware] Public stringAddressname {Get;Set; } }

Code 1-3 is the definition of ViewModel, and some of the information contained in it is unclear to see the ASP. NET MVC Model metadata series after reading this article.

Now let's look at the definition of the Controller method, code 1-4.

Code 1-4

         public ViewResult Show (Customer customer)        {            return  View (customer);        }

Why does the ViewModel want to model binding as a controller method parameter? This question will be resolved in the next article.

Take a look at code 1-5, which is the code for the view of the Show method:

Code 1-5

  @model consoleapplication2.customer@{viewbag.title = "Show";}  <  h2  >  Show</ h2    <  p  >  @Html. Editorformodel () </ p  >  <  Span style= "COLOR: #800000" >p  >  @Html. Editorfor (m=>model.address) Span style= "COLOR: #0000ff" ></ p  > 

This completes the basic work, but still cannot run, because our custom model binder has not yet been defined to the system, in the project's Global.asax file of the mvcapplication type of Application_Start () method is added as code 1-6.

Code 1-6

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

Of course, it is not limited to adding it here, as long as it is anywhere before the authorization filter is executed, because the model binder is generated after the authorization filter is executed, as explained in the next article. Adding here is just where MVC is the first to execute. Now we run to see the results.

Figure 1


Jinyuan

Source: http://blog.csdn.net/jinyuan0829

This article is copyrighted by the author and Csdn, 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.