C# 如何将ASP.NET MVC上的搜索结果打印到pdf中?
我是ASP.NET MVC的新手,我正在尝试将两个日期之间过滤的记录打印成PDF格式。我正在使用ROTATIVA生成PDF,问题是PDF生成正确,但包含所有记录,而不仅仅是两个日期前的记录筛选结果 控制器的代码:C# 如何将ASP.NET MVC上的搜索结果打印到pdf中?,c#,asp.net-mvc,razor,rotativa,C#,Asp.net Mvc,Razor,Rotativa,我是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 d
//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;
}
您调用的
SalesToPdf
操作没有参数,这意味着它将始终与未筛选列表匹配。您可以将参数传递到ActionAsPdf
重载中,如文档所示
在您的情况下,这可能需要对过滤列表执行某种单独的操作,如下所示:
[HttpPost]
公共操作结果SalesToPdf(日期时间开始日期、日期时间结束日期)
{
var report=new ActionAsPdf(“SaleList”,新{start=startDate,end=endDate});
返回报告;
}
因此,如果您有未过滤的数据,您可以调用现有操作,对于过滤后的数据,您可以调用此操作。感谢您的回复,我阅读了文档并按照您所说的做了,了解更多关于库的信息很有帮助,但现在它抛出了一个错误:
参数字典包含一个NULL条目,用于“regMVC.Controllers.SalesController”中方法“System.Web.Mvc.ActionResult SalesToPdf2(System.DateTime,System.DateTime)”的非空类型“System.DateTime”的“start”参数。可选参数必须是引用类型、可为null的类型,或者必须声明为可选参数。“Parameter name:parameters
如果视图显示按日期过滤的记录,我不知道为什么会发生这种情况,也就是说,在变量start
中,值不是空的,但在打印PDF的新方法中,它是空的……您能告诉我如何调用SalesToPdf2端点吗?”?很可能开始日期和结束日期没有正确发送。好吧,我做了两种方法,一种用于不带过滤器的打印,另一种用于带过滤器的打印(SalesToPdf2),发送日期的方式是通过视图。
@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>