One, off-topic
Continuation of the previous article:
The process of binding an action parameter in MVC
Public imodelbinder defaultbinder{ get { if (thisnull { thisnew defaultmodelbinder(); } return This . _defaultbinder; } Set { this. _defaultbinder = value; }}
Second, analysisDefaultmodelbinder
Public class defaultmodelbinder:imodelbinder{ Object bindmodel (ControllerContext controllercontext, Modelbindingcontext BindingContext){}//Other methods we are not distracted for the time being, more than this method}
Public Virtual ObjectBindmodel (ControllerContext controllercontext, Modelbindingcontext bindingcontext) {EnsureStackHelper.Ens Urestack (); if(BindingContext = =NULL) { Throw NewArgumentNullException ("BindingContext"); } BOOLFlag =false; if(!string. IsNullOrEmpty (bindingcontext.modelname) &&!BindingContext. Valueprovider. Containsprefix (Bindingcontext.modelname)) {//valueprovider is an example of Ivalueprovider, let's analyze it below. if(!bindingcontext.fallbacktoemptyprefix) {return NULL; } BindingContext=NewModelbindingcontext {modelmetadata=Bindingcontext.modelmetadata, Modelstate=bindingcontext.modelstate, PropertyFilter=Bindingcontext.propertyfilter, Valueprovider=Bindingcontext.valueprovider}; Flag=true; } if(!flag) { BOOLFlag2 =defaultmodelbinder.shouldperformrequestvalidation (ControllerContext, BindingContext); Iunvalidatedvalueprovider Arg_9b_0=Bindingcontext.unvalidatedvalueprovider; BOOLSkipvalidation =!Flag2; Valueproviderresult value=Arg_9b_0.getvalue (Bindingcontext.modelname, skipvalidation); if(Value! =NULL) { return This. Bindsimplemodel (ControllerContext, BindingContext, value); } } if(!bindingContext.ModelMetadata.IsComplexType) {return NULL; } return This. Bindcomplexmodel (ControllerContext, BindingContext);}
Look at ivalueprovider .
Public InterfaceIvalueprovider {/// <summary>Determines whether the collection contains the specified prefix.</summary> /// <returns>True if the collection contains the specified prefix; otherwise, false.</returns> /// <param name= "prefix" >The prefix to search for.</param> BOOLContainsprefix (stringprefix); /// <summary>retrieves a value object using the specified key.</summary> /// <returns>The value object for the specified key.</returns> /// <param name= "key" >The key of the value object to retrieve.</param>Valueproviderresult GetValue (stringkey); }
Take a look at this blog post: http://www.cnblogs.com/artech/archive/2012/05/17/value-provider-01.html