we know , in Asp. net MVC preview5 added a new modelbinder features , although this feature is very powerful , but not so friendly , therefore, , in the latest Asp. net MVC beta medium , The framework further strengthens this part , Enables automatic binding of complex parameters , This article fully explores the classification part of the entire MVC implement and fully explore the capabilities of the built-in modelbinder , enable MVC-based framework Web development is separated from the bitter sea of page values .
(See: Http://www.cnblogs.com/leven/archive/2008/08/31/1280481.htmlAndHttp://www.cnblogs.com/leven/archive/2008/09/01/1280862.html)
First, let's review it a bit.ModelbinderUsage.First define an object class,Then writeModelbinder,Confirm binding relationship,In this wayActionDirectly use.
However,Many of my friends have some experiences.,ThisModelbinderToo complex to use.Is there a better solution??InASP. net mvc BetaMedium,The answer is yes..That isBetaNew features provided in,Automatic Binding.
We know , In Preview5 Medium , The system can also Datetime And Nullable <datetime> Automatic Binding , However, it is powerless for custom classes. . But think about it. , Custom object classes should also be composed of a series of simple types. , That is to say , A complex entity class is composed of many simple types. , Is there a general method for binding? ? The answer is yes. , In fact, in my previous article Preview5 BoundArticleIn this example, a simple universal binding solution is provided. , Of course ,We can expand this solution. , Make it more intelligent , Want to come ASP. NET MVC The Development Team is aware of this. , So in the latest Beta In version , MVC The framework is enhanced. Defaultmodelbinder Functions , Simplify binding as much as possible .
In this article,We will explore newModelbinderHow to Use,And how it works.
First,We can do this in the fastest way."New Version"ModelbinderPractices,CreateMVCEngineering.Set a defaultRouting.Then joinCode,The project files for this practice are as follows::
After the project is created , In Newmodelbinderdemo. Library. Entities Add Simplearticle. CS. This is a simple entity for testing. , The Code is as follows: :
This is a simple entity without any modification..
Then modifyBindercontroller,JoinAction,First lookControllerAll code in:
Then 3 Items Aspx Page . Let's take a look at the key Simpletest. aspx
Pay attention to this part of the code,Form name of title and body,YesArticle. TitleAndArticle. content,Is there any rule??Not described for the moment.If you are interested, you can takeDemoResearch,In the following section, we will take a closer look at the reason for such a name..
The code in other pages will not be described in detail.,If you are interested, you can downloadDemoView by yourself.After everything above is done,Pressed with a bit of embarrassmentCrtl + F5 ....Step by step:
OK.This page is OK...But if there is a problem here,That's the description.MVCBai Xue,Bytes...
Click->
You can also come in here....But it seems that this is not the key..Enter something...
Press"Submit data"View results:
God testify,I did nothing.,Actually obtainedArticleNow....So excited..This means thatMVCUnder the Framework, developers can basically get rid of tedious page parameters.,Think about itP5UnbearableModelbinder, betaHow amiable is in the middle.
Don't be excited,A form can contain more than text fields.,The passed parameters are not only strings..This is for sure.,Let's try again.Defamodelmodelbinder ..
Add second entity. Simplearticle,The Code is as follows::
Then add the corresponding name Advancetest And Advancetestresult Of Action, The Code is as follows: :
AddAdvancetest. aspxAndAdvancetestresult. aspx,The style is consistent with the previous one.,This will no longer waste space..
Then we continue the test....
enter appropriate data , submit ..
Amazing,We still did nothing.,SystemModelbinderFully recognizedAdvancearticleClass and assign values to various types of attributes.However,Some may be worried.,The form we just added contains the time and number types.,If users do not play cards as common sense, SubmitError messages,Error??We can try:
Submit again to view results:
It seems that there is no problem.,The system uses the default value for incorrect data..But what if the default value provided by the system is different from what we need??For example, the submission time here...Obviously, it does not meet our requirements.Of course.There is a solution.,In fact, we only need to set an initial value for these attributes in the object constructor method.,SystemModelbinderIf the data cannot be filled in, it will naturally remain unchanged..
For example, modifyAdvancearticleClass:
Then, we will submit the form again.,The results are changed as follows::
Good,It automatically becomes the initial value we set..Solve this problem..
continue to think about it , it seems that we have all obtained Web text in the form , how to obtain other forms ? we continue to test other commonly used forms. , for convenience , we directly modified advancearticle class for test . the modified class is as follows :
A customEnumType to indicate the status:
Continue testingDefaultmodelbinderIQ....
Fill in Form:
Submit for viewing:
Surprisingly,The system recognizes and successfully binds data..Next let's take a look at more advanced features,Automatically bind arrays and include classes,Continue ModificationAdvancearticle:
Continue Test:
Submit to view results...Great,The system recognizes all the form information and successfully binds it..
I believe everyone has a certain understanding of the power of Automatic Parameter binding in the system.,I believe that after seeing such powerful capabilities, you want to upgrade your projects.ASP. net mvc BetaCome up and get rid of the bitter sea of Values.
In the next article,We will look at the power of the system in detailModelbinderImplementation Principle,And fine-tuning settings for default binding,Learn more about the currentMVCParameter binding feature in the framework,Attached demoDemoAllSource code.
By Leven
2008-10-18
Http://files.cnblogs.com/leven/NewModelBinderDemo.rar