У меня возникает эта проблема, когда я нажимаю ссылку для просмотра данных в формате PDF:
System.NullReferenceException: 'Object reference not set as an instance of an object.'
Эта проблема возникает, когда я пытаюсь отображать значения SQL в формате PDF.
Как видно на рисунке, есть ссылка для просмотра значений в PDF при нажатии там появится уже упомянутая ошибка.
Я делюсь кодом:
Контроллер:
public ActionResult Index(string buscarTitulo)
{
PDFPrinter db = new PDFPrinter();
ViewBag.CurrentFilter = buscarTitulo;
var datos = from s in db.SQLs
select s;
if (!String.IsNullOrEmpty(buscarTitulo))
{
datos = datos.Where(s => s.Titulo.ToString().Contains(buscarTitulo.ToUpper()));
}
return View(datos.ToList());
}
public ActionResult Pdf()
{
var reporte = new ActionAsPdf("Index");
return reporte;
}
public ActionResult Impresion(double? tit) {
using (PDFPrinter db = new PDFPrinter()) {
V_CuetaWeb v = db.SQLs.FirstOrDefault(c => c.Titulo == tit);
List<V_CuetaWeb> lista = new List<V_CuetaWeb>();
lista.Add(v);
var reporte = new PartialViewAsPdf("Pdf", v);
return reporte;
}
}
Индекс:
@model IEnumerable<ProvidusCuotas.V_CuetaWeb>
@{
ViewBag.Title = "Inicio";
}
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Inicio</title>
</head>
<body>
<form id="form">
<div>
@using (Html.BeginForm())
{
<p>
Título: @Html.TextBox("buscar", ViewBag.CurrentFilter as string)
<input type="submit" value="Filtrar" /><br />
<input type="button" value="Imprimir" onclick="window.print()" />
</p>
}
</div>
<div>
<table border="1">
@foreach (var item in Model)
{
<tr>
<th scope="row" abbr="Suscriptor">Suscriptor: </th>
<td>
<b>@Html.DisplayFor(modelItem => item.Apellido), @Html.DisplayFor(modelItem => item.Nombre)</b>
</td>
<td>Título: @Html.DisplayFor(modelItem => item.Titulo)</td>
</tr>
PDF:
@model IEnumerable<ProvidusCuotas.V_CuetaWeb>
@{
ViewBag.Title = "PDF";
}
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Inicio</title>
</head>
<body>
<form id="form">
<div>
<table border="1">
@foreach (var item in Model)
{
<tr>
<th scope="row" abbr="Domicilio">Domicilio: </th>
<td>
@Html.DisplayFor(modelItem => item.Domicilio)
</td>
<td></td>
<td></td>
<td></td>
<td>Valor Nominal: @Html.DisplayFor(modelItem => item.ValNom)</td>
</tr>
<tr>
<th scope="row" abbr="Barrio">Barrio: </th>
<td>
@Html.DisplayFor(modelItem => item.Barrio)
</td>
</tr>
<tr>
<th scope="row" abbr="Localidad">Localidad: </th>
<td>
@Html.DisplayFor(modelItem => item.Localidad)
</td>
</tr>
<tr>
<th scope="row" abbr="Telefono">Teléfono: </th>
<td>
@Html.DisplayFor(modelItem => item.Telefono)
</td>
</tr>
<tr>
<th scope="row" abbr="Celular">Celular: </th>
<td>
@Html.DisplayFor(modelItem => item.Celular)
</td>
</tr>
<tr>
<th scope="row" abbr="Descripcion">D. Plan Actual: </th>
<td>
@Html.DisplayFor(modelItem => item.DescPlanActual)
</td>
</tr>
<tr>
<th scope="row" abbr="Fecha">Fecha Sorteo: </th>
<td>
@Html.DisplayFor(modelItem => item.FechaSorteo)
</td>
</tr>
<tr>
<th>Zona: @Html.DisplayFor(modelItem => item.acidzona)</th>
<th>Cobrador: @Html.DisplayFor(modelItem => item.Cobrador)</th>
<th>Código: @Html.DisplayFor(modelItem => item.Codigo)</th>
<th>Título: @Html.DisplayFor(modelItem => item.Titulo)/@Html.DisplayFor(modelItem => item.Endoso)</th>
<th>Sorteo: @Html.DisplayFor(modelItem => item.Sorteo)</th>
<th>Cuota: @Html.DisplayFor(modelItem => item.Cuota)</th>
<th>Vencimiento: @Html.DisplayFor(modelItem => item.Vencimiento)</th>
<th>Monto: @Html.DisplayFor(modelItem => item.Monto)</th>
</tr>
}
</table>
</div>
</form>
</body>
</html>
PDFПринтер класса:
public partial class PDFPrinter : DbContext
{
public PDFPrinter()
: base("name=VisorPDF")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
//public virtual DbSet<V_CuetaWeb> V_CuetaWeb { get; set; }
public virtual DbSet<V_CuetaWeb> SQLs { get; set; }
}
Любая суеверность? как это решить? Я не понимаю ошибки