Call data from related columns from the controller

0

Good morning classmates, I have related the table students and the teacher table in SQL SERVER 2014 I am now passing to ASP.NET MVC the database with Entity Framework . I have already generated the controllers and the views and I want that from my teacher view you can put a @Html.ActionLink that takes me to a view where the student table is shown, but I do not know what data has to go in the controller to send that sight.

public class ProfesorController : Controller
    {
        private EscuelaBDEntities db = new EscuelaBDEntities();

        // GET: Profesor
        public ActionResult Index()
        {
            var Profesor = db.Profesor.Include(p => p.Estudiante).Include(p => p.Salones).Include(p => p.Instituto);
            return View(Profesor.ToList());
        }

@model IEnumerable<WebApplication1.Models.Profesor>

@{
    ViewBag.Title = "Index";
}
    
asked by senseilex 08.08.2017 в 15:12
source

1 answer

0

The student index action should filter the students by the teacher id you sent as follows:

public ActionResult Index(int id) 
{ 
var Estudiante = db.Estudiante.Where(d => e.IdProfesor==id).Include(p => 
p.Salones).Include(p => p.Instituto); 
return View(Estudiante.ToList()); 
} 

This action I would place it in a student driver and from the teacher's view I would invoke it in each teacher line something like:

@Html.ActionLink(
"Ver Estudiantes",                                // linkText
"Index",                                          // actionName
"Estudiante",                                     // controllerName
new {                                             // routeValues
    id = Model.ProfesorId
    },
null                                              // htmlAttributes

)

    
answered by 08.08.2017 / 19:24
source