MVC Ioc Builder without parameters

Asked

Viewed 878 times

6

Hello. My application is triggering an error while trying to create a new item. I’m getting this error message: No constructor without parameters has been set for this object.

But my controller class has this constructor without parameters:

public AlocacaoController()
{

}

public AlocacaoController(IAlocacaoService alocacaoService, IProfissionalService profissionalService, IContratoService contratoService)
{
            _alocacaoService = alocacaoService;
            _profissionalService = profissionalService;
            _contratoService = contratoService;
            _alocacaoMapper = new AutoMapperAlocacao();
            _contratoMapper = new AutoMapperAlocacaoContrato();
}

This error occurs before triggering this method.

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(AlocacaoViewModel model)
{
    try
    {
        //Carregar lista de clientes
        model.ListaContratos = new SelectList(_contratoService.ListarAtivos(), "ContratoID", "Nome", model.ContratoID);
        model.ListaProfissionais = new SelectList(_profissionalService.ListarAtivos(), "ProfissionalID", "Nome", null);

        if (ModelState.IsValid)
        {

        }
        return View(model);
    }
    catch
    {
        return View();
    }
}

All interfaces are configured in my dependency injection class:

private static void RegisterServices(IKernel kernel)
        {
            kernel.Bind(typeof(IBaseService<>)).To(typeof(BaseService<>));
            kernel.Bind(typeof(IAlocacaoService)).To(typeof(AlocacaoService));
            kernel.Bind(typeof(IClienteService)).To(typeof(ClienteService));
            kernel.Bind(typeof(IClienteIpService)).To(typeof(ClienteIpService));
            kernel.Bind(typeof(IClienteLocalService)).To(typeof(ClienteLocalService));
            kernel.Bind(typeof(IContratoService)).To(typeof(ContratoService));
            kernel.Bind(typeof(ILancamentoService)).To(typeof(LancamentoService));
            kernel.Bind(typeof(IProfissionalDispositivoService)).To(typeof(ProfissionalDispositivoService));
            kernel.Bind(typeof(IProfissionalService)).To(typeof(ProfissionalService));
            kernel.Bind(typeof(IUsuarioService)).To(typeof(UsuarioService));

            kernel.Bind(typeof(IBaseRepository<>)).To(typeof(BaseRepository<>));
            kernel.Bind(typeof(IAlocacaoRepository)).To(typeof(AlocacaoRepository));
            kernel.Bind(typeof(IClienteRepository)).To(typeof(ClienteRepository));
            kernel.Bind(typeof(IClienteIpRepository)).To(typeof(ClienteIpRepository));
            kernel.Bind(typeof(IClienteLocalRepository)).To(typeof(ClienteLocalRepository));
            kernel.Bind(typeof(IContratoRepository)).To(typeof(ContratoRepository));
            kernel.Bind(typeof(ILancamentoRepository)).To(typeof(LancamentoRepository));
            kernel.Bind(typeof(IProfissionalDispositivoRepository)).To(typeof(ProfissionalDispositivoRepository));
            kernel.Bind(typeof(IProfissionalRepository)).To(typeof(ProfissionalRepository));
            kernel.Bind(typeof(IUsuarioRepository)).To(typeof(UsuarioRepository));
        }  

Stack Trace:

[Missingmethodexception: No constructor without parameters has been defined for this object. ] System.RuntimeTypeHandle.Createinstance(Runtimetype type type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, Runtimemethodhandleinternal& ctor, Boolean& bNeedSecurityCheck) +0 System.RuntimeType.Createinstanceslow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, Stackcrawlmark & stackMark) +113 System.RuntimeType.Createinstancedefaultctor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, Stackcrawlmark & stackMark) +206 System.Activator.Createinstance(Type type, Boolean nonPublic) +83 System.Activator.Createinstance(Type type) +11 System.Web.Mvc.DefaultModelBinder.Createmodel(Controllercontext controllerContext, Modelbindingcontext bindingContext, Type modelType) +197

[Missingmethodexception: No constructor without parameters has been defined for this object. Object type 'System.Web.Mvc.Selectlist'. ] System.Web.Mvc.DefaultModelBinder.Createmodel(Controllercontext controllerContext, Modelbindingcontext bindingContext, Type modelType) +233 System.Web.Mvc.DefaultModelBinder.Bindsimplemodel(Controllercontext controllerContext, Modelbindingcontext bindingContext, Valueproviderresult valueProviderResult) +285 System.Web.Mvc.DefaultModelBinder.Bindmodel(Controllercontext controllerContext, Modelbindingcontext bindingContext) +299 System.Web.Mvc.DefaultModelBinder.Getpropertyvalue(Controllercontext controllerContext, Modelbindingcontext bindingContext, PropertyDescriptor propertyDescriptor, IModelBinder propertyBinder) +17 System.Web.Mvc.DefaultModelBinder.Bindproperty(Controllercontext controllerContext, Modelbindingcontext bindingContext, Propertydescriptor propertyDescriptor) +382 System.Web.Mvc.DefaultModelBinder.Bindproperties(Controllercontext controllerContext, Modelbindingcontext bindingContext) +122 System.Web.Mvc.DefaultModelBinder.Bindcomplexelementalmodel(Controllercontext controllerContext, Modelbindingcontext bindingContext, Object model) +55 System.Web.Mvc.DefaultModelBinder.Bindcomplexmodel(Controllercontext controllerContext, Modelbindingcontext bindingContext) +1230 System.Web.Mvc.DefaultModelBinder.Bindmodel(Controllercontext controllerContext, Modelbindingcontext bindingContext) +348 System.Web.Mvc.ControllerActionInvoker.Getparametervalue(Controllercontext controllerContext, Parameterdescriptor parameterDescriptor) +338 System.Web.Mvc.ControllerActionInvoker.Getparametervalues(Controllercontext controllerContext, Actiondescriptor actionDescriptor) +105 System.Web.Mvc.Async.<>c__DisplayClass21.b__19(Asynccallback asyncCallback, Object asyncState) +743 System.Web.Mvc.Async.Wrappedasyncresult1.Callbegindelegate(Asynccallback callback, Object callbackState) +14

System.Web.Mvc.Async.Wrappedasyncresultbase1.Begin(Asynccallback callback, Object state, Int32 timeout) +128 System.Web.Mvc.Async.Asynccontrolleractioninvoker.Begininvokeaction(Controllercontext controllerContext, String actionName, Asynccallback callback, Object state) +328 System.Web.Mvc.Controller.b__1c(Asynccallback asyncCallback, Object asyncState, Executaecorestate innerState) +25 System.Web.Mvc.Async.Wrappedasyncvoid1.Callbegindelegate(Asynccallback callback, Object callbackState) +30

System.Web.Mvc.Async.Wrappedasyncresultbase1.Begin(Asynccallback callback, Object state, Int32 timeout) +128 System.Web.Mvc.Controller.Beginexecutecore(Asynccallback callback, Object state) +556 System.Web.Mvc.Controller.b__14(Asynccallback asyncCallback, Object callbackState, Controller controller) +18 System.Web.Mvc.Async.Wrappedasyncvoid1.Callbegindelegate(Asynccallback callback, Object callbackState) +20

System.Web.Mvc.Async.Wrappedasyncresultbase1.Begin(Asynccallback callback, Object state, Int32 timeout) +128 System.Web.Mvc.Controller.Beginexecute(Requestcontext requestContext, Asynccallback callback, Object state) +420 System.Web.Mvc.Controller.System.Web.Mvc.Async.Iasynccontroller.Beginexecute(Requestcontext requestContext, Asynccallback callback, Object state) +16 System.Web.Mvc.MvcHandler.b__4(Asynccallback asyncCallback, Object asyncState, Processrequeststate innerState) +52 System.Web.Mvc.Async.Wrappedasyncvoid1.Callbegindelegate(Asynccallback callback, Object callbackState) +30

System.Web.Mvc.Async.Wrappedasyncresultbase1.Begin(Asynccallback callback, Object state, Int32 timeout) +128 System.Web.Mvc.MvcHandler.Beginprocessrequest(Httpcontextbase httpContext, Asynccallback callback, Object state) +369 System.Web.Mvc.MvcHandler.Beginprocessrequest(Httpcontext httpContext, Asynccallback callback, Object state) +48 System.Web.Mvc.MvcHandler.System.Web.Ihttpasynchandler.Beginprocessrequest(Httpcontext context, Asynccallback cb, Object extraData) +16 System.Web.Callhandlerexecutionstep.System.Web.HttpApplication.Iexecutionstep.Execute() +103 System.Web.Httpapplication.Executestep(Iexecutionstep step, Boolean& completedSynchronously) +155

  • Debugging my application I found that the error occurs in this get property of my model class: public virtual Selectlist Professional lists { get; set; }. This property is used to display a dropdown in my view. It works when loading the CREATE view. But the error when clicking the save button.

  • You’re using Castle Windsor?

  • No. I’m using ninject. But I don’t think the error is in the dependencies configuration, but in the coding of my Listbox control. Because the error is occurring precisely in this property Listaprofissionais that is the type Selectlist, from the framework itself.

2 answers

1

I found the problem. The operation of Listbox is a little different. When you use the Listboxfor method, it is necessary to pass two lists. One that stores the items that will be shown in the list and another list that stores the values of the selected items.

//Itens da lista
public SelectList ListaProfissionais { get; set; }
//Selecao de valores
public IEnumerable<string> Profissionais { get; set; }


@Html.ListBoxFor(model => model.Profissionais, Model.ListaProfissionais, new { style = "width:250px" })

I was passing the same list in both methods. So the error occurred. Only by the description of the error shown it is kind of hard to guess what it is. I had to catch some to find out.

0

Probably his class Alocacaoviewmodel has a constructor, but is without a standard constructor.

Add a default constructor to it, then test again:

public AlocacaoViewModel()
{

}
  • It didn’t work. The problem is in the property that feeds the listbox: public Selectlist Profissionaisselected { get; set; }

  • Ah, you can put the code of the Listratives method used to create the instance?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.