none
MVC IoC Construtor sem parametros RRS feed

  • Pergunta

  • Ola. Minha aplicação esta disparando um erro ao tentar criar um novo item. Estou recebendo esta mensagem de erro: Nenhum construtor sem parâmetros foi definido para este objeto.

    Mas minha classe controller possui esse construtor sem parametros:

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

    Esse erro ocorre antes de acionar este metodo.

    [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();
        }
    }

    Todas as interfaces estão configuradas na minha clase para injeção de dependência:

    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: Nenhum construtor sem parâmetros foi definido para este objeto.] System.RuntimeTypeHandle.CreateInstance(RuntimeType 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: Nenhum construtor sem parâmetros foi definido para este objeto. 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, ExecuteCoreState 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


    ------

    Debugando minha aplicação eu verifiquei que o erro ocorre nesta propriedade get da minha classe model:

    public virtual SelectList ListaProfissionais { get; set; }.

    Esta propriedade é utilizada para exibir uma listbox na minha view.

    Funciona quando esta carregando a view CREATE.

    Mas da erro ao clicar no botão salvar. A propriedade aparece como null

    Esta lista esta utilizando o plugin Jquery "Chosen" .

    Mas não me parece ser a razão do problema. Desabilitei o plugin, mas o erro continua ocorrendo.

    <div class="form-group">
                @Html.LabelFor(model => model.ListaProfissionais, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.ListBoxFor(model => model.ListaProfissionais, Model.ProfissionaisSelecionados, new { @class = "chosen-select", style = "width:250px" })
                </div>
            </div>
    
    
    @section Scripts {
        @Scripts.Render("~/bundles/jqueryval")
        @Scripts.Render("~/bundles/chosen")
        <script type="text/javascript">
            $(document).ready(function () {
                $(".chosen-select").chosen();
            });
        </script>
    }



    terça-feira, 26 de julho de 2016 14:02

Todas as Respostas