Sometimes we get the data from the client. Not necessarily the first thing we want, we need to do some processing. Here we take the example of a model attribute that needs to be handled.
Here are 5 ways to solve the problem.
Model
Public class mymodel{ publicstring Encrypt getset;} Public string Get Set ; }}
Controller:
Public class Homecontroller:controller { publicvoid Test (MyModel mymodel) { } }
The first way:
In the model to do the processing, very simple is also very convenient, very good understanding directly on the code
namespacewebapplication{ Public classMyModel {Private stringEncryptvalue =string. Empty; Public stringEncrypt {Set{Encryptvalue =value;} Get { returnEncryptvalue +"UUU"; } } Public stringLala {Get;Set; } }}
The second way:
Implements a default model binding override GetPropertyValue method. The property value is processed against the back. Ps:getpropertyvalue-returns the property value using the specified controller context, binding context, property descriptor, and property binder.
Preferred we want to create an attribute that marks the property that needs to be processed
Model
namespace webapplication{ publicclass mymodel { [Test] public stringgetset;} Public string Get Set ; } }}
Testattribute: Attributes used for tagging
namespace webapplication{ [AttributeUsage (Attributetargets.property,allowmultiple=false, inherited= false )] publicclass testattribute:attribute { }}
Mymodelbinder: Implementing a default model binding
Call the parent class method to get the value of the property, if the condition, type condition, and tag condition are met, the custom value is returned, the original value is returned by no
namespacewebapplication{ Public classMymodelbinder:defaultmodelbinder {protected Override ObjectGetPropertyValue (ControllerContext controllercontext, Modelbindingcontext BindingContext, System.ComponentModel.PropertyDescriptor PropertyDescriptor, Imodelbinder propertybinder) {varValue =Base. GetPropertyValue (ControllerContext, BindingContext, PropertyDescriptor, Propertybinder); if(Value is string&&propertydescriptor.attributes[typeof(Testattribute)]! =NULL) { return "I've dealt with the value."; } returnvalue; } }}
Add default model bindings
namespace webapplication{ publicclass MvcApplication:System.Web.HttpApplication { protected void Application_Start () { arearegistration.registerallareas (); Filterconfig.registerglobalfilters (globalfilters.filters); Routeconfig.registerroutes (routetable.routes); Bundleconfig.registerbundles (bundletable.bundles); Custom default model binding new mymodelbinder ();} }}
The Third Way:
Implement a custom value provider, Containsprefix method to determine whether the condition is met, Mymodel:controller action model parameter name
GetValue returns the result of the Valueproviderresult type for use with model binding
namespacewebapplication{ Public classMyvalueprovider:ivalueprovider { PublicMyvalueprovider (stringvalue) { This. Value =value; } Private stringValue {Get;Set; } Public BOOLContainsprefix (stringprefix) { if(prefix=="MyModel") { return true; } Else { return false; } } PublicValueproviderresult GetValue (stringkey) {MyModel Model=NewMyModel () {Encrypt = This. Value +"ooo" }; return NewValueproviderresult (model, model. ToString (), cultureinfo.currentculture); } }}
Implement a value provider factory: Used to provide a value provider
namespace webapplication{ publicclass myvalueproviderfactory:valueproviderfactory { publicoverride ivalueprovider Getvalueprovider (controllercontext ControllerContext) { string value = controllercontext.httpcontext.request[" Encrypt " ]; return New Myvalueprovider (value); }}}
Add a value provider factory
namespace webapplication{ publicclass MvcApplication:System.Web.HttpApplication { protected void Application_Start () { arearegistration.registerallareas (); Filterconfig.registerglobalfilters (globalfilters.filters); Routeconfig.registerroutes (routetable.routes); Bundleconfig.registerbundles (bundletable.bundles); Add a value provider factory ValueProviderFactories.Factories.Add (New Myvalueproviderfactory ()); } }}
Fourth Way:
Implement a custom value provider factory, Mymodel:controller action model parameter name
MyModel as the encrypt prefix as the NameValueCollection key to save, as the parameters of the Namevaluecollectionvalueprovider
The model is tree-like, but the namevaluecollection is planar, so the data is saved as a prefix.
namespacewebapplication{ Public classMyvalueproviderfactory:valueproviderfactory { Public Overrideivalueprovider Getvalueprovider (ControllerContext controllercontext) {varValue = controllercontext.httpcontext.request["Encrypt"]; if(Value = =NULL) return NULL; NameValueCollection NameValueCollection=NewNameValueCollection (); Namevaluecollection.add ("Mymodel.encrypt","AAA"+value); return NewNamevaluecollectionvalueprovider (NameValueCollection, CultureInfo.InvariantCulture); } }}
Add a value provider factory
namespace webapplication{ publicclass MvcApplication:System.Web.HttpApplication { protected void Application_Start () { arearegistration.registerallareas (); Filterconfig.registerglobalfilters (globalfilters.filters); Routeconfig.registerroutes (routetable.routes); Bundleconfig.registerbundles (bundletable.bundles); Add a value provider factory ValueProviderFactories.Factories.Add (new myvalueproviderfactory ());} } }
Fifth way:
Implements a custom model binding, gets the current value in BindingContext, and returns a model after processing
namespacewebapplication{ Public classMybinder:imodelbinder { Public ObjectBindmodel (ControllerContext controllercontext, Modelbindingcontext bindingcontext) {varValue = BindingContext.ValueProvider.GetValue ("Encrypt"). Attemptedvalue; return NewMyModel {Encrypt = value +"Ooxx" }; } }}
Specifying model bindings using the Modelbinder attribute
namespace webapplication.controllers{ class Homecontroller:controller { void Test ([Modelbinder (typeof(Mybinder))] MyModel MyModel) {} }}
Learning and progress is a generation of things, we should constantly strive to see a bigger and different world.
May 2016, can live happily, less trouble, do more excellent projects and products
Model binding several ways to handle data sent by clients