【问题标题】:How to print into a pdf the results of a search on ASP.NET MVC?如何将 ASP.NET MVC 上的搜索结果打印成 pdf?
【发布时间】:2020-11-03 23:11:04
【问题描述】:

我是 ASP.NET MVC 的新手,我正在尝试将两个日期之间的过滤记录打印到 PDF。我正在使用 ROTATIVA 生成 PDF,问题是 PDF 生成正确但包含所有记录,而不仅仅是两个日期的记录过滤结果。

控制器代码:

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

我不知道该怎么做,欢迎提出任何建议。

*更新 ---> 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>

【问题讨论】:

    标签: c# asp.net-mvc razor rotativa


    【解决方案1】:

    您正在调用不带参数的 SalesToPdf 操作,这意味着它将始终匹配未过滤的列表。您可以将参数传递给ActionAsPdf 重载,如文档here中所示

    在您的情况下,这可能需要对过滤列表执行某种单独的操作,如下所示:

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

    因此,如果您有未过滤的数据,您将调用您现有的操作,而对于过滤的数据,您将调用此操作。

    【讨论】:

    • 感谢您的回复,我阅读了文档并按照您所说的做了,了解更多有关该库的信息很有帮助,但现在它会抛出错误:The parameter dictionary contains a NULL entry for the 'start' parameter of the non-nullable type 'System.DateTime' of the method 'System.Web.Mvc.ActionResult SalesToPdf2 (System.DateTime, System.DateTime)' in 'regMVC .Controllers.SalesController '. An optional parameter must be a reference type, a nullable type, or it must be declared as an optional parameter." Parameter name: parameters
    • 如果视图显示按日期过滤的记录,不知道为什么会出现这种情况,即变量start中的值不为null,而是在打印PDF的新方法中它是...
    • 您能告诉我您是如何调用 SalesToPdf2 端点的吗?可能没有正确发送开始日期和结束日期。
    • 好的,所以,我做了两种方法,一种是不带过滤器的打印,另一种是带过滤器的打印(SalesToPdf2),发送日期的方式是通过视图。
    猜你喜欢
    • 1970-01-01
    • 2014-08-03
    • 2010-12-13
    • 2017-09-29
    • 1970-01-01
    • 1970-01-01
    • 2016-09-09
    • 2017-10-01
    • 1970-01-01
    相关资源
    最近更新 更多