简体   繁体   中英

Parameters in ASP.NET MVC to create a PDF

I have a particular problem, I am working with ASP.NET MVC in C# and SQL Server. The idea is that on the main screen you see a text box and a button, entering a number that brings us our data. By bringing that data, the person can choose whether they want to see if that data in a PDF. But, when I try to carry out those data to the pdf the problem arises that I will show.

Index:

<div>
        <form method="post" action="/Home/ChRoja">
            <p>Titulo: <input type="text" name="titulo" /></p>

            <p><input type="submit" value="Chequera Roja" /></p>
        </form>
    </div>

Controller:

public ActionResult Index()
   {
       return View();
   }

   public ActionResult ChRoja() 
   {
       ConexionSQL cn = new ConexionSQL();
       double titulo = Convert.ToDouble(Request.Form["titulo"].ToString());
       return View(cn.cargarDatos(titulo));
   }

   public ActionResult Pdf()
   {
       double titulo = Convert.ToDouble(Request.Form["id"].ToString());
       return new Rotativa.ActionAsPdf("ChRoja", titulo);
   }

ChRoja:

<body style='background: url(/images/CHEQUERAS-ROJAS-CORTA.png) no-repeat center'>
    <div>
        <form id="form">

            <div>
                <table back>
                    @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>
                            <td>
                                @Html.ActionLink("Ver detalles en PDF", "Pdf", new { id = item.Titulo })
                            </td>
                        </tr>
                    }
                </table>
            </div>
        </form>

        <a href="@Url.Action("Pdf", "Home")">Convertir a Pdf</a>
    </div>
</body>

So the problem is that when I press to view the PDF values of the query the following happens:

System.NullReferenceException: 'Referencia a objeto no establecida como instancia de un objeto.'

在此处输入图像描述

Any suggestions?

you are using the wrong constructor for Html.ActionLink. There is an answer here .

Solution for your case is to add a parameter "Home" for the controller's action, as well as null at the end for htmlattributes.

@Html.ActionLink("Ver detalles en PDF", "Pdf", "Home", new { id = item.Titulo }, null)

Solution: Change this

@Html.ActionLink("Ver detalles en PDF", "Pdf", new { id = item.Titulo })

For this:

@Html.ActionLink("Ver detalles en PDF", "Pdf", new { titulo = item.Titulo })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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