Return list in an array - Webapi C#

Asked

Viewed 169 times

1

Good guys. I need to return a list of courses linked to a particular student, this list will be consumed through the get of a Webapi.

I have the classes "student", "course" and "cursoAluno" (tables in the base - Entityframework).

the student table does not have the course id, the course table does not have the student id, hence I created the course Student, in it I have the Id(Identity), idAluno, idCurso.

I need that when consuming webApi is returned the following structure:

{
"id": 2,
"nome": "João Silva",
"endereco": "RUA que sobe e desce",
"mensalidade": 0,
"cursos": [
    {
        "id": 2,
        "nome": "Portugês"
    },
    {
        "id": 5,
        "nome": "Matemática",
    }
]
},

I changed the class to :

public class Aluno {
public int Id { get; set; }
public string Nome  { get; set; }
public string Endereco  { get; set; }
public double Mensalidade  { get; set; }
public List<Curso> Cursos { get; set; }
}

and at the time of popular the list I did so:

var aluno = dbContext.Alunos
    .Include("Cursos")//Irá carregar os cursos vinculados aos alunos
    .Find(id);//Obtém apenas um aluno

But it’s not returned the way I need it. Can someone help me with this ? I am using Webapi C#

Grateful.

  • 1

    But it is not returned the way I need it. Can anyone help me in this ? I am using Webapi C# ...... is returning what?

  • Good.. Your model is right with Json informed. take a look here : http://json2csharp.com/ It will generate the corresponding model for your Json ai and it is right.. Probably your call there is something wrong.. paste here HOW is coming wrong! abs.

  • Shows the course class as well. You are using the correct Entity Framework?

No answers

Browser other questions tagged

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