How to list related data

2

In my project I have two tables: Student and Occurrences. And they are related so that a student can have multiple occurrences. My need is: In the student Details view (Details.cshtml), I need to show all occurrences related to the student. That is, all occurrences that relate to his name. I've tried in many ways, but I can not do that. I'm already a little lost, I've already looked for the mistakes I get, but nothing that helps me. I've tried using partials, but since the list of occurrences is an IEnumerable, it gives Models conflict when loading the detail view. Could anyone give me a how-to about 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 (Student Occurrences)

@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>
    
asked by anonymous 30.05.2014 / 21:07

1 answer

2

The explicit load of occurrences has been missing. Modify your Action details to the following:

[PermissionAttribute("Administrador,Coordenador")]
// GET: /Alunos/Detalhes/5
public ActionResult Detalhes(long? id)
{
    if (id == null)
    {
        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
    }

    Aluno aluno = db.Alunos.Include(a => a.Ocorrencias).SingleOrDefault(id);

    if (aluno == null)
    {
        return HttpNotFound();
    }

    return View(aluno);
}

The Include method tells the Entity Framework that you will explicitly load student instances into the search.

If I'm not mistaken, Include does not work with Find .

    
30.05.2014 / 21:45