I have a table of funcionário
and a table of projeto
related many to many as shown in the diagram below:
OnthecontrollerFuncionariosController
intheDetalhes
methodIwanttolistthelistofprojectsthatareinvolved.Inthemethodpopuloamodelandsendtotheviewasbelow:
publicIActionResultDetails(intid){varmodel=newFuncionario();using(varDBContext=newDadosProjetosContext()){model=DBContext.Funcionario.Include(a=>a.FuncionarioProjeto).Where(a=>a.FuncionarioId==id).Single();}returnView(model);}
IntheviewI'mdoingthefollowingforeach
tolisttheprojects:
<divclass="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 error System.NullReferenceException: Object reference not set to an instance of an object.
when calling the page. How do I fix this?