Extension point you must know in Asp.net MVC2 (2): Model Binder

Source: Internet
Author: User

Model binder is very simple in Asp.net MVC. Simply put, the Action Method in your Controller requires parameter data. The parameter data is contained in the HTTP request,

Including values in the form and parameters in the URL. The modelbinder function is to replace the values and URL parameters in these forms with objects, and then bind these objects

To the parameter of the action. I simply drew a picture, which looks more intuitive.

 

In Asp.net MVC, you can write something similar to the following:Code:

 
[Httppost]PublicActionresultCreate (){BookBook =NewBook(); Book. Title = request. Form ["Title"];//...ReturnView ();}

However, this writing method is very undesirable because the code is not easy to read or test. Let's look at the following statement:

[Httppost]PublicActionresultCreate (FormcollectionValues ){BookBook =NewBook(); Book. Title = values ["Sex"];//...ReturnView ();}

In this way, you do not need to obtain data from the request. This satisfies some situations and is more intuitive than directly obtaining data from the request. However

The data must come from both the value on the form and the query string from the URL. This case aloneFormcollectionNo. See the following code:

[Httppost]PublicActionresultCreate (BookBook ){//...ReturnView ();} the code above is very intuitive. This requires our model binder to create a book object and then directly take values from the attributes of this object. Number of book objects
 
It is also from form and URL. Sometimes, our defamodelmodelbinder transformation capabilities must be limited and transparent. In some special and complex situations, we need
 
Customize model binder. Next I will talk about how to customize model binder.

1. First, we define the entity class of a book:

Public classBook{Public StringTitle {Get;Set;}Public StringAuthor {Get;Set;}PublicDatetimeDatepublished {Get;Set;}}

2. The custom model binder must inherit the imodelbinder or its subclass. Data can be obtained from bindingcontext.

 Public class  Bookmodelbinder : Imodelbinder { Public object Bindmodel ( Controllercontext Controllercontext, Modelbindingcontext Bindingcontext ){ VaR Book = ( Book ) (Bindingcontext. Model ?? New  Book (); Book. Title = getvalue < String > (Bindingcontext, "Title" ); Book. Author = getvalue < String > (Bindingcontext, "Author" ); Book. datepublished = getvalue <Datetime > (Bindingcontext, "Datepublished" ); If ( String . Isnullorempty (book. Title) {bindingcontext. modelstate. addmodelerror ( "Title" , "Cannot the title be blank? " );} Return Book ;} Private T getvalue <t> ( Modelbindingcontext Bindingcontext, String Key ){Valueproviderresult Valueresult = bindingcontext. valueprovider. getvalue (key); bindingcontext. modelstate. setmodelvalue (Key, valueresult ); Return (T) valueresult. convertize ( Typeof (T ));}}

 
From the code above, we can see that the custom modelbinde is very free and can freely map a key on the form to an attribute of the entity,
 
You can also add some verification logic. Of course, you can also add some other custom logic.
 
3. After writing bookmodelbinder, just register it and add the following code in global. asax:
 
Modelbinders. Binders. Add (Typeof(Book),NewBookmodelbinder());

Summary:This article briefly introduces the model Binder Mechanism of Asp.net MVC. If you have any questions, please correct them.

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.