Partial does not render

Asked

Viewed 188 times

4

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

Here 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 that 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:

  • _Add occurrence.cshtml
  • Adds.cshtml
  • Details.cshtml
  • Edits.cshtml
  • Index.cshtml
  • Removes.cshtml

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

Shared:

Inside Shared I have the directory Editortemplates and it contains:

  • Collection.cshtml

Dentro de Shared:

  • _Layout.cshtml
  • _Loginpartial.cshtml
  • Error.cshtml

1 answer

3


You forgot to pass the Model inside Partial. See the line @Html.Partial down 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 won’t even appear. 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>

Browser other questions tagged

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