Em meu projeto tenho duas tabelas: Aluno e Ocorrencias. E elas estão relacionadas de forma que um aluno pode ter várias ocorrências. A minha necessidade é: Na view de Detalhes do aluno(Detalhes.cshtml), preciso mostrar todas as ocorrências relacionadas ao aluno. Ou seja, todas as ocorrências que tenham relação ao nome dele. Já tentei de várias formas só que não estou conseguindo fazer isso. Já estou meio perdido, já procurei pelos erros que recebo, mas nada que me ajude. Já tentei usar partials, mas como fazer a lista de ocorrências é um IEnumerable, dá conflito de Models ao carregar a view de detalhes. Será que alguém poderia me dar um norte de como fazer isso ?
Controller(Aluno)
// GET: /Alunos/Detalhes/5
public ActionResult Detalhes(long? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Aluno aluno = db.Alunos.Find(id);
if (aluno == null)
{
return HttpNotFound();
}
return View(aluno);
}
View(Detalhes)
@model CEF01.Models.Aluno
@{
ViewBag.Title = "Detalhes";
}
<div>
<dl class="dl-horizontal">
<dt>
@Html.DisplayNameFor(model => model.Nome)
</dt>
<dd>
@Html.DisplayFor(model => model.Nome) <br />
</dd>
<dt>
@Html.DisplayNameFor(model => model.Foto)
</dt>
<dd>
<img src="@Model.Foto" border="0" width="150px" height="160px"/>
</dd>
<dt>
@Html.DisplayNameFor(model => model.NomePai)
</dt>
<dd>
@Html.DisplayFor(model => model.NomePai)<br />
</dd>
<dt>
@Html.DisplayNameFor(model => model.NomeMae)
</dt>
<dd>
@Html.DisplayFor(model => model.NomeMae)<br />
</dd>
<dt>
@Html.DisplayNameFor(model => model.NomeResponsavel)
</dt>
<dd>
@Html.DisplayFor(model => model.NomeResponsavel)<br />
</dd>
<dt>
@Html.DisplayNameFor(model => model.Endereco)
</dt>
<dd>
@Html.DisplayFor(model => model.Endereco)<br />
</dd>
<dt>
@Html.DisplayNameFor(model => model.DataDeNascimento)
</dt>
<dd>
@Html.DisplayFor(model => model.DataDeNascimento)<br />
</dd>
<dt>
@Html.DisplayNameFor(model => model.AnoLetivo)
</dt>
<dd>
@Html.DisplayFor(model => model.AnoLetivo)<br />
</dd>
<dt>
@Html.DisplayNameFor(model => model.Ano)
</dt>
<dd>
@Html.DisplayFor(model => model.Ano)<br />
</dd>
<dt>
@Html.DisplayNameFor(model => model.Turma)
</dt>
<dd>
@Html.DisplayFor(model => model.Turma)<br />
</dd>
<dt>
@Html.DisplayNameFor(model => model.Numero)
</dt>
<dd>
@Html.DisplayFor(model => model.Numero)<br />
</dd>
<dt>
@Html.DisplayNameFor(model => model.Turno)
</dt>
<dd>
@Html.DisplayFor(model => model.Turno)<br />
</dd>
<dt>
@Html.DisplayNameFor(model => model.Telefone)
</dt>
<dd>
@Html.DisplayFor(model => model.Telefone)<br />
</dd>
<dt>
@Html.DisplayNameFor(model => model.TelefoneContato)
</dt>
<dd>
@Html.DisplayFor(model => model.TelefoneContato)<br />
</dd>
<dt>
@Html.DisplayNameFor(model => model.TelefoneResponsavel)
</dt>
<dd>
@Html.DisplayFor(model => model.TelefoneResponsavel)<br />
</dd>
</dl>
</div>
<br />
<br />
<br />
<p>
@Html.ActionLink("Editar", "Edita", new { id = Model.Id }) |
@Html.ActionLink("Voltar para lista", "Index")
</p>
<h3>Ocorrencias</h3>
@Html.Partial("PartialDetalhesOcorrencias", Model.Ocorrencias)
Partial(Ocorrencias do Aluno)
@model IEnumerable<CEF01.Models.Ocorrencia>
@{
}
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.Aluno.Nome)
</th>
<th>
@Html.DisplayNameFor(model => model.Tipo)
</th>
<th>
@Html.DisplayNameFor(model => model.Causa)
</th>
<th>
@Html.DisplayNameFor(model => model.Descricao)
</th>
<th></th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Aluno.Nome)
</td>
<td>
@Html.DisplayFor(modelItem => item.Tipo)
</td>
<td>
@Html.DisplayFor(modelItem => item.Causa)
</td>
<td>
@Html.DisplayFor(modelItem => item.Descricao)
</td>
</tr>
}
</table>