Skip to content
Advertisement

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.’

enter image description here

Any suggestions?

Advertisement

Answer

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)
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement