0

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?

6
  • 2
    Does this answer your question? how to pass parameter from @Url.Action to controller function Commented Apr 13, 2020 at 12:14
  • no, what i need is for the pdf to be created. But what I mentioned happens @SelimYıldız Commented Apr 13, 2020 at 12:26
  • 2
    It seems the problem is not about creating pdf. Obviously your problem is that you don't send id to server as a parameter. So Request.Form["id"] throws null exception. Commented Apr 13, 2020 at 12:28
  • 1
    You're not even getting to the PDF creation line. You need to solve the issue in the line that's actually failing. Then you can start looking at the PDF. BTW it's possible to do this on the client side. Commented Apr 13, 2020 at 12:47
  • Yes but how to send id as a parameter? I tried solve with the url how to you say me but not work to me @SelimYıldız Commented Apr 13, 2020 at 17:27

2 Answers 2

1

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)
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.