views:

47

answers:

1

This is my custom model binder. I have my breakpoint set at BindModel but does not get fired with this controller action:

public ActionResult Create(TabGroup tabGroup)

...

public class BaseContentObjectCommonPropertiesBinder : DefaultModelBinder
{
    public new object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        if (controllerContext == null)
        {
            throw new ArgumentNullException("controllerContext");
        }
        if (bindingContext == null)
        {
            throw new ArgumentNullException("bindingContext");
        }

        BaseContentObject obj = (BaseContentObject)base.BindModel(controllerContext, bindingContext);
        obj.Modified = DateTime.Now;
        obj.Created = DateTime.Now;
        obj.ModifiedBy = obj.CreatedBy = controllerContext.HttpContext.User.Identity.Name;
        return obj;
    }

My registration:

// tried both of these two lines

ModelBinders.Binders[typeof(TabGroup)] = new BaseContentObjectCommonPropertiesBinder();
ModelBinders.Binders.Add(typeof(TabGroup), new BaseContentObjectCommonPropertiesBinder());
+2  A: 

It's because you used "new" keyword on BindModel method. The "new" means that method will not participate in virtual invocation (more about polymorphism you can read here.

Replace "new" with "override" and it should work fine.

public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
    //your stuff
}
Misha N.