我建立这种格式的链接:http://localhost:33333/Invoices/Reports?format=pdf
当我运行这显示此错误:
System.InvalidOperationException:传入字典的模型项的类型为'System.Data.Entity.Infrastructure.DbQuery 1[<>f__AnonymousType9
11[Webb.Models.Faktury,System.Int32,System.String,System.Nullable 1[System.DateTime],System.Nullable
1[System.DateTime], System.Nullable 1[System.Single],System.Nullable
1[System.Int32],System.String,System.Nullable 1[System.Single],System.Nullable
1[System.Single],System.Nullable 1[System.Single]]]', but this dictionary requires a model item of type 'System.Collections.Generic.List
1[Webb.Models.Faktury]'。
看法:
@model List<Webb.Models.Faktury>
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<h2>Html Report</h2>
<p>
Lorem ipsum dolor sit amet</p>
<table width="100%">
<tr>
<td>User Name</td>
<td>Description</td>
<td>Lucky Number</td>
</tr>
@foreach (var item in Model)
{
<tr>
<td>@item.FAK_Id</td>
<td>................</td>
<td>@item.FAK_Numer</td>
</tr>
}
</table>
</body>
</html>
控制器:
// Setup sample model
var pro = (from a in db.Fakturies
join b in db.Wierszes on a.FAK_Id equals b.WIE_Fkid
join c in db.Produkties on b.WIE_Pid equals c.PRO_Id
select new
{
a,
a.FAK_Id,
a.FAK_Numer,
a.FAK_DataS,
a.FAK_TerminZ,
a.FAK_Rabat,
b.WIE_Ilosc,
c.PRO_Nazwa,
c.PRO_CenaN,
c.PRO_CenaB,
c.PRO_Vat
});
pro = pro.Where(a => a.FAK_Id == 6);
// Output to Pdf?
if (Request.QueryString["format"] == "pdf")
return new PdfResult(pro, "Reports");
return View(pro);
}
我应该怎么做才能使用数据库中的数据将成功视图导出为 pdf?
--编辑1:
public ActionResult Reports(int? id)
{
// Setup sample model
var pro = (from a in db.Fakturies
join b in db.Wierszes on a.FAK_Id equals b.WIE_Fkid
join c in db.Produkties on b.WIE_Pid equals c.PRO_Id
select a);
pro = pro.Where(a => a.FAK_Id == id);
var vm = new PrintViewModel();
vm.Fakturies = pro; //assuming pro is already loaded with the above code.
vm.Wierszes = db.Wierszes;
if (Request.QueryString["format"] == "pdf")
return new PdfResult(vm, "Reports");
return View(vm);
}