I'm having a problem in my application where I wanted to use partial to render one page inside another. That is, one controller inside another. But what happens is the partial does not render at all and I've done almost everything, but it does not appear.
Here is the part of the code that calls partial:
<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>
Directories and their 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
Controller
private EntidadesContext db; //= new EntidadesContext();
public OcorrenciasController(EntidadesContext contexto)
{
this.db = contexto;
}
public ActionResult Index()
{
var ocorrencias = db.Ocorrencias.Include(o => o.Aluno);
return View(ocorrencias.ToList());
}
public ActionResult Adiciona(long id) /* Esse Id é de Aluno, não de Ocorrencia */
{
var aluno = db.Alunos.SingleOrDefault(a => a.Id == id);
var ocorrencia = new Ocorrencia
{
Aluno = aluno
};
db.SaveChanges();
return View(ocorrencia);
}
public ActionResult Edita(Ocorrencia ocorrencia)
{
db.Entry(ocorrencia).State = EntityState.Modified;
db.SaveChanges();
return View(ocorrencia);
}
public ActionResult Remove(long? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Ocorrencia ocorrencia = db.Ocorrencias.Find(id);
if (ocorrencia == null)
{
return HttpNotFound();
}
db.SaveChanges();
return View(ocorrencia);
}