Well, I'm having a problem here to generate a pdf with the data coming from the bank. My system has a student register and its occurrences. What I wanted is that, when detailing the data, I could print these data on the screen (the data of the student and the occurrences related to it), because when you detail a student, it is a specific chosen of the Index (total list of students). By clicking 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/gerando-relatorios-em-pdf-com-rotativa-w7/, but I think I'm wrong, generating this error here:
Object reference not set to an instance of an object.
In this 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 the basis for mounting 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 encountered when searching for student image: @ Html.DisplayNameFor (model => model.Foto)
The error happens as soon as I click on the link to display in the browser the pdf to be printed.
Can anyone help me?