Method (Data) of my Controller:
// GET: datos
public ActionResult Datos()
{
var datos = db.alumno.Include(a => a.grupo);
ViewBag.mensaje = "HOLA MUNDO";
var query = from Alumno in db.alumno
join Grupo in db.grupo on Alumno.id_alumno equals Grupo.id_alumno
select new { alumno = Alumno, grupo = Grupo };
return View(query);
}
View where the data should be displayed
@model IEnumerable<WebApplication1.grupo>
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Datos</title>
</head>
<body>
<div>
@foreach (var item in Model)
{
@item.id_grupo <br />
}
@ViewBag.mensaje
</div>
</body>
</html>
Your variable
datos
is a list or collection of objects of the typeAlumno
, what you should do is:And in your View:
To cycle through them:
EDITION
Regarding the fact that you changed the query, I help you
Since now you don't return a list of a specific object but a list with JOIN. You must do the following:
create a MODEL
Then in your Controller :
In this Join code block, we create our object with the attributes we need, I made those fields to give you an example and also because I don't know the data you need.
And finally in your View
Update your code as follows:
Controller
View (in case it
datos
is not a collection)View (in case it
datos
is a collection)