Partial does not render

4

I have a problem with my application. What happens is that I am not able to render a partial in another view.

Here is the partial code:

@model CEF01.Models.Ocorrencia

@using (Html.BeginCollectionItem("Ocorrencia"))
{
    @Html.HiddenFor(model => model.AlunoId)

    <div class="form-horizontal">
        <h4>Ocorrencia</h4>
        <hr />
        @Html.ValidationSummary(true)

        <div class="form-group">
            @Html.LabelFor(model => model.Tipo, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Tipo)
                @Html.ValidationMessageFor(model => model.Tipo)
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Causa, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Causa)
                @Html.ValidationMessageFor(model => model.Causa)
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Observacao, new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Observacao)
                @Html.ValidationMessageFor(model => model.Observacao)
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Salvar" class="btn btn-default" />
            </div>
        </div>
    </div>
}

And here's the part where I try to render this partial in another view:

<div class="panel-group" id="accordion">
<div class="panel panel-default">
    <div class="panel-heading">
        <h4 class="panel-title">
            <a data-toggle="collapse" data-parent="#accordion" href="#ocorrencias">
                Ocorrências do Aluno
            </a>
        </h4>
    </div>

    <div id="ocorrencias" class="panel-collapse collapse in">
        <div class="panel-body">
            @foreach (var ocorrencia in Model.Ocorrencias)
            {
                @Html.Partial("_AdicionaOcorrencia")
            }
        </div>
    </div>
</div>

Directories and their respective files: Students:

  • _AdditionAcurrent.cshtml
  • Add.cshtml
  • Details.cshtml
  • Edita.cshtml
  • Index.cshtml
  • Remove.cshtml

Occurrences:  - Add.cshtml  - Details.cshtml  - Edita.cshtml  - Index.cshtml  - Remove.cshtml

Shared:

Inside Shared I have the EditorTemplates directory and it contains:

  • Collection.cshtml

Inside Shared:

  • _Layout.cshtml
  • _LoginPartial.cshtml
  • Error.cshtml
asked by anonymous 15.05.2014 / 01:54

1 answer

3

You forgot to move the Model into Partial. See the @Html.Partial line below:

<div id="ocorrencias" class="panel-collapse collapse in">
    <div class="panel-body">
        @foreach (var ocorrencia in Model.Ocorrencias)
        {
            @Html.Partial("_AdicionaOcorrencia", ocorrencia)
        }
    </div>
</div>

Another thing is if Model.Ocorrencias is empty. Then it will not come. What you can do is:

<div id="ocorrencias" class="panel-collapse collapse in">
    <div class="panel-body">
        @if (Model.Ocorrencias.Count > 0) {
            foreach (var ocorrencia in Model.Ocorrencias)
            {
                @Html.Partial("_AdicionaOcorrencia", ocorrencia)
            }
        } else {
            <div>Ainda não há ocorrências</div>
        }
    </div>
</div>
    
15.05.2014 / 01:55