I have a class called AlumnoViewModel and a controller called AlumnoesController but when I leave the fields that I want to list in the controller it gives me an error telling me that I cannot convert implicitly,
this is my controller
// GET: api/Alumnoes/listar
[HttpGet("[action]")]
public async Task<ActionResult<IEnumerable<Alumno>>> Listar()
{
var alumno = await _context.Alumnos.ToListAsync();
return alumno.Select(c => new AlumnoViewModel
{
id_alumno = c.id_alumno,
nombre = c.nombre,
apellido = c.apellido,
calle_numero = c.calle_numero,
colonia = c.colonia,
ciudad_estado = c.ciudad_estado,
lada = c.lada,
Telefono = c.Telefono,
pais = c.pais,
codigo_postal = c.codigo_postal,
correo_institucional = c.correo_institucional,
sexo = c.sexo,
nacionalidad = c.nacionalidad,
estado = c.estado
});
}
In the Action it is defined that you are going to return a
IEnumerable<Alumno>
However, when returning the values you are returned through the select a
IEnumerable<AlumnoViewModel>
To fix it your code should look like this