user2964094
user2964094

Reputation:

asp.net mvc ModelBinder always null

i've model binder in my asp.net mvc 4 site.

public class OptionModelBinder : System.Web.Mvc.IModelBinder
        {         
            private const string SessionKey = "Opt";
            public object BindModel(ControllerContext controllercontext, System.Web.Mvc.ModelBindingContext bindingContext)
            {

                    Option opt = (Option)controllercontext.HttpContext.Session[SessionKey];
                    if (opt == null)
                    {
                        opt = new Option ();
                        controllercontext.HttpContext.Session[SessionKey] = opt ;

                    }
                    return opt;
                 }
        }

And Controller:

publick ActionResult Index(Option currentOption)
{
//currentOption always null
}

I'm sure it worked before. Something happened because of the recent updates Solution. I do not understand what is wrong. Please help.

Upvotes: 0

Views: 153

Answers (1)

Ilya Sulimanov
Ilya Sulimanov

Reputation: 7836

Check Application_Start method in Global.asax.cs Make sure that there is that something like a

ModelBinders.Binders.Add (typeof (Option), new OptionModelBinder ());

it is necessarily

Upvotes: 1

Related Questions