简体   繁体   English

使用RazorPDF错误转换视图

[英]Error convert view using RazorPDF

I build this format of link: http://localhost:33333/Invoices/Reports?format=pdf 我建立了这种链接格式: 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.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]'. 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]” 1[System.Single]]]', but this dictionary requires a model item of type 'System.Collections.Generic.List

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? 我应该如何使用数据库中的数据将成功视图导出为pdf?

--EDIT 1: -编辑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 . 您的观点与Faktury的集合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 Faktury class. 更改LINQ表达式代码以使用Faktury类进行投影。

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 == 6);

 if (Request.QueryString["format"] == "pdf")
      return new PdfResult(pro.ToList(), "Reports");

return View(pro);

If you need to pass more information than the list of Fakturties, you may create a view model with the properties you need. 如果您需要传递比Fakturties列表更多的信息,则可以使用所需的属性创建视图模型。

public class PrintViewModel
{
  public IEnumerable<Faktury> Fakturies {set;get;}
  public IEnumerable<Wiersz> Wierszes {set;get;}  

  public PrintViewModel()
  {
    this.Fakturies = new List<Faktury>();
    this.Wierszes = new List<Wiersz>();
  }
}

and in your action method, create an object of this, assign the property values and send to the view. 然后在您的操作方法中,创建一个对象,分配属性值并将其发送到视图。

var vm=new PrintViewModel();
vm.Fakturies=pro;  //assuming pro is already loaded with the above code.
vm.Wierszes =db.Wierszes;
return View(vm);

Now make sure that your view is strongly typed to this new view model 现在,请确保您的视图已严格键入此新视图模型

@model PrintViewModel
<h3>Fatturies</h3>
@foreach(var f in Model.Fakturies)
{ 
  <p>@f.FAK_Numer</p>
}
<h3>Wierszes</h3>
@foreach(var f in Model.Wierszes)
{ 
  <p>@f.Name</p>
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM