2
In my project I have two tables: Student and Occurrences. And they are related so that a student can have several occurrences. My need is: In the Student Details view (Details.cshtml), I need to show all student-related occurrences. That is, all the occurrences that relate to his name. I have tried in several ways only that I am not able to do it. I’m already a little lost, I’ve looked for the mistakes I get, but nothing that helps me. I have tried using partials, but how to make the list of occurrences is a Ienumerable, gives conflict of Models when loading the detail view. Could someone give me a north of how to do this ?
Controller(Student)
// 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(Details)
@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(Occurrences of the Student)
@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>
You can put us the code of the Controller?
– Leonel Sanches da Silva
It’s the normal way it comes from scaffolding. I scaffolding and managed the views. Just to try to put these occurrences data to trying to use partial view. And in it is the Ienumerable, but not working.
– Érik Thiago
Then enter the Controller and Views code generated, please.
– Leonel Sanches da Silva
Done! The changes are there !
– Érik Thiago