System.Missingmethodexception: No constructor without parameters has been defined for this object

Asked

Viewed 247 times

0

I’m getting the following error:

System.Missingmethodexception: No constructor without parameters has been defined for this object.

My Controller:

using BecomexTeste.Models;
using BecomexTeste.Models.BracoViewModel;
using BecomexTeste.Service;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Globalization;
using System.Linq;
using System.Web;
 using System.Web.Mvc;

 namespace BecomexTeste.Controllers
{
[Route("Braco")]
public class BracoController : Controller
{
    private readonly IBracoService service;

    public BracoController(IBracoService service)
    {
        this.service = service;
    }

    [HttpGet]
    public JsonResult GetBraco()
    {
        var braco = service.BuscaBraco();

        return new JsonResult { Data = braco, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
    }

    [HttpPost]
    public ActionResult Update(UpdateBracoViewModel model)
    {
        if (ModelState.IsValid)
        {
            var braco = new Braco
            {
                CotoveloDireito  = model.CotoveloDireito,
                PulsoDireito     = model.PulsoDireito,
                CotoveloEsquerdo = model.CotoveloEsquerdo,
                PulsoEsquerdo    = model.PulsoEsquerdo,
            };

            service.UpdateBraco(braco);
        }

        return View("~/Views/Home/Index.cshtml", model);
    }
}

JS that should fetch data View:

@section scripts{
<script>
    $(document).ready(function () {
        var braco  = [];
        var cabeca = [];

        BuscaDadosCabecaBraco();

        function BuscaDadosCabecaBraco() {
            braco  = [];
            cabeca = [];

            $.ajax({
                type: "GET",
                url: '@Url.Action("GetBraco", "Braco")',
                success: function (data) {
                    $.each(data, function (i, v) {
                        braco.push({
                            CotoveloEsquerdo: v.CotoveloEsquerdo,
                            PulsoEsquerdo:    v.PulsoEsquerdo,
                            CotoveloDireito:  v.CotoveloDireito,
                            PulsoDireito:     v.PulsoDireito
                        });
                    });
                },
                error: function (error) {
                    alert('Não foi possivel carregar dados do braço');
                }
            });

            $.ajax({
                type: "GET",
                url: '@Url.Action("GetCabeca", "Cabeca")',
                success: function (data) {
                    $.each(data, function (i, v) {
                        cabeca.push({
                            Rotacao:    v.CotoveloEsquerdo,
                            Inclinacao: v.PulsoDireito
                        });
                    });
                },
                error: function (error) {
                    alert('Não foi possivel carregar dados da cabeça');
                }
            });
        }

        $('.geraMovimentoCabeca').on('click', function (event) {
            var button = $(event.relatedTarget);

            var cabeca = button.data('cabeca');

            if (cabeca == "inclinacao") {
                var inclinacao = button.data('inclinacao');
            } else {
                var rotacao = button.data('rotacao');
            }

            if (typeof inclinacao !== 'undefined') {
                cabeca.Inclinacao = inclinacao;
            } else {
                cabeca.Rotacao = rotacao;
            }

            $.ajax({
                type: "POST",
                url: "/Cabeca/Update",
                data: {
                    "Rotacao":    cabeca.Rotacao,
                    "Inclinacao": cabeca.Inclinacao
                },
                success: function (data) {
                    alert("Enviou");
                },
                error: function (error) {
                    alert('Não foi possivel enviar dados da cabeça');
                }
            });
        });
    });
</script>

}

Interface Service:

public interface IBracoService
{
    void UpdateBraco(Braco braco);
    Braco BuscaBraco();
}

And the service itself:

public class BracoService : IBracoService
{
    private Banco banco;

    public BracoService(Banco _banco)
    {
        this.banco = _banco;
    }

    public void UpdateBraco(Braco Braco)
    {
        Braco BracoBanco = BuscaBraco();

        BracoBanco.CotoveloDireito  = Braco.CotoveloDireito;
        BracoBanco.PulsoDireito     = Braco.PulsoDireito;
        BracoBanco.CotoveloEsquerdo = Braco.CotoveloEsquerdo;
        BracoBanco.PulsoEsquerdo    = Braco.PulsoEsquerdo;

        this.banco.SaveChanges();
    }

    public Braco BuscaBraco()
    {
        Braco BracoEncontrada = this.banco.Bracos.FirstOrDefault();

        return BracoEncontrada;
    }
}
  • is . Net Core? you are using Dependency Injection?

  • is web Api, I’m trying to use dependency injection yes

  • This error is because failed to start the IBracoService in your Injection framework

  • Could you tell me how do I inject dependency ? and if it would be in the archive WebApiConfig

  • This article may help, https://medium.com/balta-io/caso-de-uso-inje%C3%A7%C3%A3o-de-depend%C3%Aancia-com-simple-injector-no-mvc-e-web-api-51ce647efd1a

  • I couldn’t implement

  • Ideal is you open a question with doubts and where you had the problem.

Show 2 more comments
No answers

Browser other questions tagged

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