I build this format of link: http://localhost:33333/Invoices/Reports?format=pdf
When I run this shows this error:
System.InvalidOperationException: The model item passed into the dictionary is of type 'System.Data.Entity.Infrastructure.DbQuery1[<>f__AnonymousType911[Webb.Models.Faktury,System.Int32,System.String,System.Nullable1[System.DateTime],System.Nullable1[System.DateTime],System.Nullable1[System.Single],System.Nullable1[System.Int32],System.String,System.Nullable1[System.Single],System.Nullable1[System.Single],System.Nullable1[System.Single]]]', but this dictionary requires a model item of type 'System.Collections.Generic.List1[Webb.Models.Faktury]'.
View:
@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>
Controller:
// 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);
}
What should I do to export success view to pdf with data from database?
--EDIT 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);
}
Your view is strongly typed to a collection of
Faktury. But from your action method you are passing a collection of annonymous objects.Change your LINQ expression code to do a projection using the
Fakturyclass.If you need to pass more information than the list of Fakturties, you may create a view model with the properties you need.
and in your action method, create an object of this, assign the property values and send to the view.
Now make sure that your view is strongly typed to this new view model