简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM