ASP. net mvc: implement your custom modelbinder

Source: Internet
Author: User

Environment:

Windows 2008, vs 2008 SP1, ASP. net mvc RC1

------------------------------------------------------------------------------

Above:ASP. net mvc: Model Binding Mechanism AnalysisThe process of model binding in ASP. net mvc is very rough. This article will explore how to implement a modelbinder of your own, and further demonstrate some of the details ignored above.

Suppose we have a book class,

Now we want to add the book function. Therefore, create a bookcontroller and add an action:

Create a view:


In fact, at this step, when we submit the form, we can get the book automatic binding in the action, thanks to the built-in defamodelmodelbinder. However, we want to use the modelbinder we created based on some considerations. Then we create a simplemodelbinder class which implements the imodelbinder interface (this interface must be implemented by the custom modelbinder ).
Imodelbinder has only one method bindmodel (). This method accepts two parameters: controllercontext and modelbindingcontext. Therefore, when implementing this method, we need to take full advantage of the information contained in these two parameters.

Now let's take a look at the specific implementation of simplemodelbinder:


Above, I try to avoid hard encoding, because I hope this simplemodelbinder has a certain degree of versatility. In addition, the chain effect of Model Field name change is also avoided. Here, we use bindingcontext. modeltype to create a model instance, use typedescriptor to obtain the property set of the model, use bindingcontext. valueprovider to obtain the form information of the request, and finally return a model instance.

In this way, we have created simplemodelbinder. So how can we get it started?

So I came up with a question: how does ASP. NET MVC determine which modelbinder to be applied currently?

Answer: in order: (1) attribute of the model parameter in the Action method, (2) modelbinders. Binders dictionary, (3) attribute of the model class, (4) defamodelmodelbinder

That is to say, we have three methods (1) (2) (3) to tell ASP. net mvc which modelbinder to which model we expect to apply at present.

(1) Add attribute to the model parameter of action:


(2) Add simplemodelbinder to the modelbinders. Binders dictionary:


(3) add attribute to book class definition:


Okay, you can choose one of the three methods.

Here, we have created a very simple custom modelbinder, but do you know that it also lacks a very important function, that is, filter the model property during binding, because I have ignored the bindattribute member for a long time. Next, let's talk about bindattrider and make some improvements to simplemodelbinder.

In addition, defamodelmodelbinder is very powerful. When you need to apply a custom modelbinder, you need to consider it in the actual project.

Copyright

Author: Tristan g
This article is copyrighted by the author and the blog site.

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.