In ASP.NET MVC 4, is there a way I can add a custom DateTime model binder for a single action? -


i'm trying bind form model parameter in controller. model contains datetime field , i'd bound when form submitted. form field expects date input in non-standard format (which can't change - various reasons).

the controller action is:

public actionresult registration_post(registrationdetails model) 

i need custom bind (datetime) dateofbirth property in registrationdetails class. rest of properties work default binding.

i don't want override datetime binding whole app - single action (or, if easier, controller).

any idea how can this? tried using modelbinder attribute on action, in:

public actionresult registration_post([modelbinder(typeof(customdatetimebinder)]registrationdetails model) 

however, appears need create custom binder whole registrationdetails class, seems overkill.

also, i'd prefer not put custom format on model property, class used elsewhere, polluting class.

i'm using mvc4.

can tell me best way of handing this?

try this: create custom model binder provider.

in bindmodel method have add logic deal requirement date of birth coming registration_post action has special format. btw, need bind whole model.

using system; using system.web.mvc; using mvcapp.models;  public class custommodelbinderprovider : imodelbinderprovider  {     public imodelbinder getbinder(type modeltype)      {         return modeltype == typeof(person) ? new personmodelbinder() : null;     } }  protected void application_start() {         modelbinderproviders.binderproviders.add(new custommodelbinderprovider());     }   public class personmodelbinder : imodelbinder  {     public object bindmodel(controllercontext controllercontext, modelbindingcontext                                bindingcontext)     {          //add logic here bind person object    } 

Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -