6
I’m having a problem in my application where I wanted to use partial to render one page inside another. That is, one controller within another. Only, 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 where the code that calls the 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 respective files:
Students:
- _Add occurrence.cshtml
- Adds.cshtml
- Details.cshtml
- Edits.cshtml
- Index.cshtml
- Removes.cshtml
Occurrences:
- Adds.cshtml
- Details.cshtml
- Edits.cshtml
- Index.cshtml
- Removes.cshtml
Shared:
Inside Shared I have the directory Editortemplates and it contains:
- Collection.cshtml
Dentro de 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);
}
Tries
@Html.Partial("/Alunos/_AdicionaOcorrencia", ocorrencia)
– CesarMiguel
And what mistake it makes?
– CesarMiguel
I’ve done it this way. And if I put it this way, it gives error saying that it does not find the file. I have already put the partial up in the Shared folder and it does not think at all. And it just does not give any error, only it does not appear the partial. Only the text that there in the div inside the Else.
– Érik Thiago
So, there’s nothing at all, because I didn’t populate the occurrences part because I couldn’t render the partial to test and register something. It’s strange, because it should be rendering, because this partial is to register and not to list... I didn’t even do it yet, because I’m trying to understand why in a being appearing nothing.
– Érik Thiago
I don’t understand anything anymore. You’re not passing items on the model, so how do you expect him not to go to Else?
– CesarMiguel
What happens is, I can not register anything in the model, because the partial view I did to make the registration does not load, that’s why I did not register anything. Or rather, let me try another way, @Cesarmiguel would I be able to do a partial view to list ? And then render this other partial to register ?
– Érik Thiago
Check the Model.Occurrences come without data. Put a breackpoint in the controller and see if it comes with data
– CesarMiguel
I’ve done it too. And it doesn’t stop, it goes right through, it’s like I don’t have the breakpoint. I don’t understand anything. Do I have to create a partial class for it to render ? I’ve been researching and I saw that there is this partial class.
– Érik Thiago
Put your controller code in the question to take a look
– CesarMiguel
Changes made, present controller !
– Érik Thiago
Do you have any student data in the comic book? You must not have
– CesarMiguel
The table is already there. But I didn’t register anything because I’m not able to open the partial view... In other words, I have the Students table, which already has data, and the table of occurrences that has nothing. But the student table already has the reference for the occurrences as well as the occurrences has the reference for students. But the case is that I’m not able to render the partial to register and I don’t know why.
– Érik Thiago
So it’s explained! You’re making a query where you check for students in the occurrences, as you don’t have the Model goes to empty... When you check in the view the Model no if, and as it has nothing goes to Else. Go to the database and make there an occurrence to the "nail" and you will see that it can. You should follow some tutorials before doing a project in your head...
– CesarMiguel
Is there a link that I can use as a reference so I do this action there in the bank ?
– Érik Thiago