2
Guys, I’m having a problem here to generate a pdf with the data coming from the bank. My system has a register of students and their occurrences. What I wanted was that, at the time of detailing the data, I could print the data that already comes on the screen (the student’s data and the occurrences related to it), because when detailing a student, it is a specific chosen from the Index (total list of students). Highlighting here, I’m using the Rotativaw7 library, which supports Brazilian special characters !
What happens is that I did everything based on a tutorial that has this link:
http://cleytonferrari.com/generatingreports-em-pdf-com-rotary w7/, but I think I’m wrong, causing this error here:
Undefined object reference for an object instance.
In that exact line:
var model = new Student
I’ll put the code I’m using here:
Controller
public ActionResult GeraPDF(long? id)
{
Aluno aluno = db
.Alunos
.Include(x => x.Ocorrencias)
.AsNoTracking()
.FirstOrDefault(f => f.Id == id);
var pdf = new ViewAsPdf
{
ViewName = "GeraPDF",
Model = modelo,
PageSize = Size.A4,
PageMargins = new Margins { Bottom = 2, Left = 2, Right = 2, Top = 2}
};
return pdf;
}
View(used as a base to build the pdf)
@model CEF01.Models.Aluno
<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 />
<hr />
<h3>Ocorrências</h3>
<br />
@Html.Partial("PartialDetalhesOcorrencias", Model.Ocorrencias)
Error when searching for student image: @Html.Displaynamefor(model => model. Photo)
The error happens as soon as I click on the link to display the pdf in the browser to be printed.
I wonder if someone could help me ?
What is the need to create another object? You can’t pass the
aluno
directly in Model?– Leonel Sanches da Silva
So, I don’t know, because I’m following the tutrial that is on the link, and according to the tutorial, creates this other object... I didn’t understand either, but I was following right.
– Érik Thiago
Try to pass
aluno
directly and see what happens.– Leonel Sanches da Silva
taking this part "...new Student." and leaving only the licking part to pull the data and the right occurrences ?
– Érik Thiago
That’s right. I’ll put it in answer.
– Leonel Sanches da Silva
All right, I’m trying here !
– Érik Thiago
Eric, our issues collided, check out the arrangements I made, it is better to use Markdown instead of HTML
– brasofilo
Gee man, it was really worth ! Fight, I even tried to use, but I guess I still don’t understand, and thanks for clarifying better !
– Érik Thiago