简体   繁体   中英

How to print into a pdf the results of a search on ASP.NET MVC?

I'm new to ASP.NET MVC and I am trying to print the filtered records between two dates to a PDF. I'm using ROTATIVA to generate the PDF, the problem is that the PDF is generated correctly but with all the records and not only with the result of the record filter by the two dates.

Code for the Controller:

    //this method is for put the list of the records on the view
    public ActionResult SaleList()
    {
        using (inventoryEntitiesDBA dc = new inventoryEntitiesDBA())
        {
            return View(dc.sales.ToList());
        }
    }

    //this method is to filter the records between start and end date
    [HttpPost]
    public ActionResult SaleList(DateTime start, DateTime end)
    {
        bool Status = false;
        if(ModelStatus.IsValid())
        {
            using(inventoryEntitiesDBA dc = new inventoryEntitiesDBA())
            {
                 var d = dc.sales.Where(x => x.sale_day >= start && x.sale_day <= end).ToList();
              
                 Status = true;

                 ViewBag.Status = Status;

                 return View(d);
            }
        } 
    }

    //this method is to generate the PDF
    public ActionResult SalesToPdf()
    {
        var report = new ActionAsPdf("SaleList");

        return report;
    }

I don't know exactly what to do, any suggestions are appreciated.

*UPDATE ---> view :

@if (ViewBag.Status != null && Convert.ToBoolean(ViewBag.Status))
{
    //this is for print if the method for filter
    //the dates are called
    <p align="right">
         @Html.ActionLink("Generate PDF", "SalesToPdf2")
    </p>
}
else
{
    //this is for print if the method for filter the dates are not called
    <p align="right">
         @Html.ActionLink("Generate PDF", "SalesToPdf")
    </p>
}

<center>
    @using (Html.BeginForm("SaleList", "Sales", FormMethod.Post))
    {
        
        <span>Start Date </span> <input type="date" name="start" />
        <span> End Date </span><input type="date" name="end" />
        <input type="submit" value="Filter" />
    }
</center>

You're calling the SalesToPdf action with no parameter which means it will always match the unfiltered list. You can pass parameters into the ActionAsPdf overloads as shown in the documentation here

In your case this would probably require some kind of separate action for the filtered list like this:

    [HttpPost]
    public ActionResult SalesToPdf(DateTime startDate, DateTime endDate)
    {
        var report = new ActionAsPdf("SaleList", new {start=startDate, end=endDate});

        return report;
    }

So if you have unfiltered data you'd call your existing action, and for filtered data you'd call this one.

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