How to make the Return Ok<Object> take multiple arguments

Asked

Viewed 99 times

1

Good afternoon guys, I would like to ask you a question, I am creating a method within a webapi in Asp.net mvc to receive some data from the launch of a home visit, but I’m having an error in Return Ok < Object >, that tells me that the method Okay< Object > can not receive more than 1 argument, as I am new in programming so there are some mistakes that for now I can not solve, if anyone can help me I will be very grateful. Good afternoon to all!!

//CAPTURAR VISITAS
    [HttpGet]
    [Route("GetVisitas/{grupoId}/{userId}")]
    public IHttpActionResult GetVisitas(int grupoId, int userId)
    {


        var visita_periodica = false;
        var cadastro_atualizacao = false;
        var consulta = false;
        var exame = false;
        var vacina = false;
        var gestante = false;
        var recemNascido = false;
        var desnutricao = false;
        var deficiencia = false;
        var hipertenso = false;
        var diabetes = false;
        var asma = false;
        var acamado = false;
        var tuberculose = false;
        var cancer = false;
        var imovelComFoco = false;
        var acaoMecanico = false;
        var realizada = false;
        var recusada = false;
        var ausente = false;

        var visitas = db.GruposDetalhes.Where(gd => gd.GrupoId == grupoId && gd.UserId == userId).ToList();

        foreach (var visita in visitas)
        {
            foreach (var visita2 in visita.Visitas)
            {
                visita_periodica = visita2.Visita_periodica;
                cadastro_atualizacao = visita2.Cadastro_atualizacao;
                consulta = visita2.Consulta;
                exame = visita2.Exame;
                vacina = visita2.Vacina;
                gestante = visita2.Gestante;
                recemNascido = visita2.RecemNascido;
                desnutricao = visita2.Desnutricao;
                deficiencia = visita2.Deficiencia;
                hipertenso = visita2.Hipertenso;
                diabetes = visita2.Diabetes;
                asma = visita2.Asma;
                acamado = visita2.Acamado;
                tuberculose = visita2.Tuberculose;
                cancer = visita2.Cancer;
                imovelComFoco = visita2.ImovelComFoco;
                acaoMecanico = visita2.AcaoMecanico;
                realizada = visita2.Realizada;
                recusada = visita2.Recusada;
                ausente = visita2.Ausente;
            }
        }

        return Ok<object>(
            new { Visitas = visita_periodica },
            new { Visitas = cadastro_atualizacao },
            new { Visitas = consulta },
            new { Visitas = exame },
            new { Visitas = vacina },
            new { Visitas = gestante },
            new { Visitas = recemNascido },
            new { Visitas = desnutricao },
            new { Visitas = deficiencia },
            new { Visitas = hipertenso },
            new { Visitas = diabetes },
            new { Visitas = asma },
            new { Visitas = acamado },
            new { Visitas = tuberculose },
            new { Visitas = cancer },
            new { Visitas = imovelComFoco },
            new { Visitas = acaoMecanico },
            new { Visitas = realizada },
            new { Visitas = recusada },
            new { Visitas = ausente });
    }

//ORIGINAL TUTORIAL METHOD I AM FOLLOWING

//CAPTURAR NOTAAS
    [HttpGet]
    [Route("GetNotas/{grupoId}/{userId}")]
    public IHttpActionResult GetNotas(int grupoId, int userId)
    {
        var notaDef = 0.0;
        var notas = db.GruposDetalhes.Where(gd => gd.GrupoId == grupoId && gd.UserId == userId).ToList();
        foreach(var nota in notas)
        {
            foreach(var nota2 in nota.Notas)
            {
                notaDef += nota2.Percentual + nota2.Nota;
            }
        }
        return Ok<object>(new { Notas = notaDef});               
    }
  • vc can create the object before opening to a variable and simply return return Ok(variavel)

1 answer

1

You are not returning an object, but rather a collection... And the method Ok() cannot receive a generic argument of type.

Below follows an alternative, far from being the most elegant form...

return Ok( new List<object>{
               new { Visitas = visita_periodica },
               new { Visitas = cadastro_atualizacao },
               new { Visitas = consulta },
               new { Visitas = exame },
               new { Visitas = vacina },
               // ...
           });
  • Mt thanks Leandro Angelo, I will try to implement this way !!

  • @Ricardo Now explain to me what in fact you are trying to do, because the rest of your code and the return you will get don’t make much sense...

  • So Leandro, in vdd by the tutorial that I am following to implement in my project is a little different, because it was created to make a calculation and then I tried to implement in my case, however in my case I will not make calculation, I will only return the recorded data prior to the visit, so of the 2 loops is, but I know that in my case it should not be that way but as I am beginner so I do not have enough knowledge to change the method, I’ll put the method as it is in the tutorial for you understand right.

Browser other questions tagged

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