How to list related data

Asked

Viewed 220 times

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?

  • 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.

  • Then enter the Controller and Views code generated, please.

  • Done! The changes are there !

1 answer

2


Explicit load of occurrences was missing. Modify your Action of details for 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 method Include indicates to the Entity Framework that you will explicitly load the student’s occurrences in the survey.

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

  • Dai I can charge the partial with a foreach showing ?

  • @Érikthiago That’s right.

  • My dear, I’m going to test it here ! Because I’m on time with it and I can’t make it work ! Let me see here ! Just one thing, I’ll put my partial to charge, see if it’s right ?

  • Yes, only update the question if it goes wrong I update the answer in sequence.

  • 1

    Worked here !

Browser other questions tagged

You are not signed in. Login or sign up in order to post.