也许从 DefaultModelBinder 继承并通过 Ninject 解析模型类?
更新:
NinjectModelBinder.cs
public class NinjectModelBinder : DefaultModelBinder
{
private readonly StandardKernel _kernel;
public NinjectModelBinder(StandardKernel kernel)
{
_kernel = kernel;
}
protected override object CreateModel(ControllerContext controllerContext,
ModelBindingContext bindingContext, Type modelType)
{
var model = _kernel.TryGet(modelType);
if (model != null) return model;
return base.CreateModel(controllerContext, bindingContext, modelType);
}
}
全球.asax.cs
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
var kernel = new StandardKernel();
ModelBinders.Binders.DefaultBinder = new NinjectModelBinder(kernel);
}
}