2
I am having problems in the relationship of my bank, I am developing an application that manages Courses, I am still beginner in Asp.net MVC. I have two tables Pupil and Course, and I have another table that makes the association between these two tables Hallucinogenic.
Model Aluno
public class Aluno
{
[Key]
public int Id { get; set; }
public string Nome { get; set; }
...
public virtual ICollection<AlunoCurso> AlunoCursos { get; set; } }
Model Curso
public class Curso
{
[Key]
public int Id { get; set; }
public string Nome_Curso { get; set; }
...
public virtual Aluno Aluno { get; set; }
public ICollection<AlunoCurso> AlunoCursos { get; set; } }
Model Alunocurso
public class AlunoCurso
{
[Key]
public int Id { get; set; }
public int AlunoId { get; set; }
public int CursoId { get; set; }
public bool Aprovado { get; set; }
public virtual Aluno Aluno { get; set; }
public virtual Curso Curso { get; set; }
}
The problem is that the column Alunoid on the table of Course is not receiving the Id student’s.
But in the association table Hallucinogenic is receiving the value of the field Alunoid.
List of Student Courses
public ActionResult MeusCursos()
{
Aluno aluno = db.Alunos.FirstOrDefault(a => a.Usuario == User.Identity.Name);
if (aluno != null)
return View("MeusCursos", db.Cursos.ToList());
return View();}
View Meuscursos
@model IEnumerable<MeuProjeto.Models.Curso>
@{
Layout = "/Views/Shared/_Layout.cshtml";
}
<h2>Meus Cursos</h2>
<table class="table table-hover">
<tr>
<th>
Curso
</th>
<th>
Aluno
</th>
<th>
Aprovado?
</th>
<th></th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Nome_Curso)
</td>
<td>
@Html.DisplayFor(modelItem => item.AlunoCursos)
</td>
<td>
@Html.DisplayFor(modelItem => item.Aprovado)
</td>
<td>
<div class="btn-group">
<div class="col-md-offset-2 col-md-10">
@if (item.Aprovado == false)
{
<input type="submit" value="Pendente de Aprovação" name="meusCursos" class="cursos btn btn-success" disabled="disabled" data-id="@item.Id"/>
}
else
{
<input type="submit" value="Emitir Declaração" name="meusCursos" class="cursos btn btn-default" enable="enable" />
}
</div>
</div>
</td>
</tr>
}
</table>
Listing Screen
But what happens @Gypsy is when I list the courses that the student is enrolled, the name of the student comes with nothing, because of this Alunoid.
– Novato
@Newbie And you’re listing that name as?
– Leonel Sanches da Silva
I edited the question @Gypsy is coming so blank the field. When I put the number of the Alunoid on the table Course on the bench, he filled the field Pupil in View , but doing so came all student data and not only his name, understood?!
– Novato
@Newbie Edited the answer.
– Leonel Sanches da Silva
Gave error @Gypsy, this mine Action is from the controller Course. I made that change you indicated but in mine View gives error in this line also @foreach (var item in Model.Alunocursos), it does not recognize the Alunocursos. I want to list the Course, The Name of the Student, and Approved.
– Novato
@Newbie You’re using wrong.
Curso
has to do withAlunoCursos
, andAlunoCurso
has to do withAluno
. If you want to list the courses of aAluno
, you have to select theAluno
and use the propertiesvirtual
ofAluno
, even if it is a Controller course.– Leonel Sanches da Silva
So this one of mine Action goes inside the controller Pupil @Gypsy ?
– Novato
@Newbie It’d be better if it was.
– Leonel Sanches da Silva
Fine, I’ll try to solve according to your reply @Gypsy. If I can’t open another detailing better?
– Novato
@Yes, of course! Make yourself at home.
– Leonel Sanches da Silva