0

I'm trying to bind a form to a model parameter in a controller. The model contains a DateTime field and I'd like that to be bound when the form is submitted. The form field expects the date to be input in a non-standard format (which I can't change - for various reasons).

The controller action is:

public ActionResult Registration_Post(RegistrationDetails model)

I only need to custom bind a (DateTime) DateOfBirth property in the RegistrationDetails class. All the rest of the properties work with the default binding.

I don't want to override the DateTime binding for the whole app - just for this single action (or, if easier, controller).

Any idea how I can do this? I tried using the ModelBinder attribute on the action, as in:

public ActionResult Registration_Post([ModelBinder(typeof(CustomDateTimeBinder)]RegistrationDetails model)

However, it appears that I need to create a custom binder for the whole RegistrationDetails class, which seems like overkill.

Also, I'd prefer not to put the custom format on the model property, as the class is used elsewhere, so I be polluting the class.

I'm using MVC4.

Can anyone tell me the best way of handing this?

dommer
  • 19,610
  • 14
  • 75
  • 137
  • Placing the attribute directly on the parameter is supposed to work. What do you get -- just no custom binder behavior? – David Tansey Apr 05 '13 at 16:45
  • The parameters dictionary contains an invalid entry for parameter 'model' for method 'System.Web.Mvc.ActionResult Registration_Post(WebUi.Domain.RegistrationDetails)' in 'WebUi.Controllers.AccountController'. The dictionary contains a value of type 'System.DateTime', but the parameter requires a value of type 'WebUi.Domain.RegistrationDetails'. Parameter name: parameters – dommer Apr 05 '13 at 17:10
  • It sounds like you're returning a `DateTime` from the Custom Binder, when you need to return the expected type `RegistrationDetails`. I realize that your 'special requirement' only applies to one field within the model but you've got to deal with model as a whole... – David Tansey Apr 05 '13 at 17:25

1 Answers1

0

Try this: create a custom model binder provider.

In your BindModel method you will have to add logic to deal with the requirement that only the date of birth coming from your Registration_Post action has a special format. Btw, you need to bind the 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 to bind the person object
   }
Flavia
  • 563
  • 4
  • 9