0
I have a table of funcionário
and a table of projeto
related to many to many as shows diagram below:
At the controller FuncionariosController
in the method Detalhes
i want to list the list of projects they are involved in. In the method populate the model and send to the view as below:
public IActionResult Details(int id)
{
var model = new Funcionario();
using( var DBContext = new DadosProjetosContext() ) {
model = DBContext.Funcionario
.Include(a => a.FuncionarioProjeto)
.Where(a => a.FuncionarioId == id )
.Single()
;}
return View(model);
}
In the view I’m doing the following foreach
to list the projects:
<div class="container">
@foreach (var FP in Model.FuncionarioProjeto) {
<a asp-controller="Projetos" asp-action="Detalhes" asp-route-id="@FP.Projeto.ProjetoId" class="btn-bracketed">
@FP.Projeto.Nome
</a>
<br>
}
</div>
But I’m getting the bug System.NullReferenceException: Object reference not set to an instance of an object.
when calling the page. How to solve this problem?