この形式のリンクを作成します: 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);
}