Why can't I add my custom model binder in my global.asax folder?
Asked Answered
H

1

8

I see examples all over the net of people setting up their custom model binders like this:

// global.asax
protected void Application_Start()
{
    ModelBinders.Binders.Add(typeof(YourModel), new YourBinder());
}

But when I try that, it doesn't compile (.Binders isn't found). What gives?

Hollister answered 24/8, 2012 at 15:5 Comment(0)
H
13

It turns out this was just a naming conflict because I had put my custom model binder in a folder/namespace called "ModelBinders". You can fix this one of two ways:

  1. Rename the namespace/folder to something else, e.g. CustomModelBinders
  2. Use a fully qualified reference to the ModelBinders like this:

    System.Web.Mvc.ModelBinders.Binders.Add( /* ... */ );

Hollister answered 24/8, 2012 at 15:5 Comment(1)
Thanks Michael It works. Don't know the reason why but it's running.Bertold

© 2022 - 2024 — McMap. All rights reserved.