开发者

Custom Model binder not firing

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 (control开发者_JS百科lerContext == 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());


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
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜