简体   繁体   English

Null 参考 Model

[英]Null reference in Model

I am having this problem when i press a link to view data in pdf:当我按下链接查看 pdf 中的数据时,我遇到了这个问题:

在此处输入图像描述

System.NullReferenceException: 'Object reference not set as an instance of an object.'

This problem arises when I try to display the SQL values in PDF.当我尝试在 PDF 中显示 SQL 值时会出现此问题。

在此处输入图像描述

As you can see in the image, there is a link to see the values in PDF, when you click there the error already mentioned will appear.正如您在图像中看到的,有一个链接可以查看 PDF 中的值,当您单击此处时,将出现已经提到的错误。

I share the code:我分享代码:

Controller: Controller:

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;
            }
        }

Index:指数:

@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: 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>

PDFPrinter class: PDF打印机 class:

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; }
    }

Any sugerence?有什么好意思吗? how to solve this?如何解决这个问题? I don't understan the mistake我不明白错误

I think your flow needs to change, so index will pass list of pdf objects.我认为您的流程需要更改,因此索引将传递 pdf 对象列表。 and it iterate from view and display a table.它从视图中迭代并显示一个表格。 This part correct.这部分正确。

Then you click from one link, then it should pass that click object to second view.然后从一个链接单击,然后它应该将单击 object 传递到第二个视图。 Then it will display you object values.然后它将显示 object 值。

So pdf view model should be所以 pdf 查看 model 应该是

@model ProvidusCuotas.V_CuetaWeb

Then no need to have foreach loop.然后不需要有 foreach 循环。 Just access the properties and display those.只需访问属性并显示它们。

and you c# method should be like this而你的 c# 方法应该是这样的

public ActionResult Impresion(double? tit) {
    using (PDFPrinter db = new PDFPrinter()) 
    {
        V_CuetaWeb v = db.SQLs.FirstOrDefault(c => c.Titulo == tit);
        return View("Pdf", v);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM