0

我最近使用 Visual Studio 2010 为我的 MVC 应用程序创建了我的第一个 RDLC 报告。

我使用 Report Wizard 创建 Report1.rdlc 并运行以下控制器代码将输出呈现为 PDF。

报表运行时我的数据源引用了我的模型中的站点对象。一切运行良好,但输出中的 4 列中有 2 列呈现为 #Error

两列都有每条记录的数据,没有空值

使 2 #Error 列与呈现的列不同的唯一原因是它们比模型低一级。即4列:

第一个字段 SiteDescription 呈现正常 第二个字段 SiteOperator 将每行的值呈现为 #Error 第三个字段 SiteStatus 将每行的值呈现为 #Error 第四个字段 CapacityMW 呈现正常

注意:SiteOperator 是 Site.SiteOperator.Operator。第三场类似。第一个和第四个是 Site 表中的字段(即本例中的顶层模型)

问题:开发人员还需要做些什么来允许 RDLC 使用模型中的现有对象吗?即我注意到报告向导创建了一个 Report1.rdlc.xml 文件,也许这必须修改,我已经没有这个想法了。任何评论都非常感谢

这是我的控制器代码:

        private void RenderReport(string ReportPath, object Model)
    {

        var localReport = new LocalReport { ReportPath = ReportPath };

        var reportDataSource = new ReportDataSource("DataSet1", Model);
        localReport.DataSources.Add(reportDataSource);

        var reportType = "PDF";
        string mimeType;
        string encoding;
        string fileNameExtension;

        //The DeviceInfo settings should be changed based on the reportType
        //http://msdn2.microsoft.com/en-us/library/ms155397.aspx
        string deviceInfo =
        "<DeviceInfo>" +
        "  <OutputFormat>PDF</OutputFormat>" +
        "  <PageWidth>8.5in</PageWidth>" +
        "  <PageHeight>11in</PageHeight>" +
        "  <MarginTop>0.5in</MarginTop>" +
        "  <MarginLeft>1in</MarginLeft>" +
        "  <MarginRight>1in</MarginRight>" +
        "  <MarginBottom>0.5in</MarginBottom>" +
        "</DeviceInfo>";

        Warning[] warnings;
        string[] streams;
        byte[] renderedBytes;

        //Render the report
        renderedBytes = localReport.Render(
            reportType,
            deviceInfo,
            out mimeType,
            out encoding,
            out fileNameExtension,
            out streams,
            out warnings);

        //Clear the response stream and write the bytes to the outputstream
        //Set content-disposition to "attachment" so that user is prompted to take an action
        //on the file (open or save)
        Response.Clear();
        Response.ContentType = mimeType;
        Response.AddHeader("content-disposition", "attachment; filename=foo." + fileNameExtension);
        Response.BinaryWrite(renderedBytes);
        Response.End();
    }
4

3 回答 3

0

我在 MVC 中使用 RDLC 和源自实体框架生成的 C# 类的模型(使用 POCO 对象模板)。

现在,我的类是一个相对“扁平”的类,只有属性(可空值、数字、字符串、日期时间等),没有其他对象的集合,因此可能会有所作为。

我在没有使用向导的情况下创建了我的 RDLC,但发现要使用 MVC 中的设计器来执行此操作,我必须在项目中添加一个 .aspx 以供设计器工作。我也只是在一个模拟类中有公共静态方法,它返回我用于报表数据源向导等使用的类型的 IEnumerables。

我还使用 EF 来填充我的数据,因此它绝对不需要存储过程:

var data = dal.PrintFriendlies.Where( p => p.ApplicationId == applicationId );
viewer.LocalReport.DataSources.Add( new ReportDataSource( "DataSourceName" , data ) );

我还将返回 PDF 字节,因此您的代码看起来与我使用的非常接近(我同时使用数据源和参数,以及子报告)。

你没有描述你的类结构是什么样的,但我认为它存在层次结构的问题,所以你可能需要将它展平为报告作者喜欢的东西。

于 2012-07-30T17:08:50.570 回答
0

在 RDLC 中接收“#Error”响应时首先要查看的是 Visual Studio 中的“输出/调试”选项卡。对于报告引擎在呈现报告时收到的每个错误,这应该有一个特定的错误消息。虽然并不总是世界上最直观的信息,但它们至少应该为您指明正确的方向。

于 2012-07-25T16:37:54.920 回答
0

我设法让报告运作良好。我切换到使用存储过程并从控制器调用它。我将 RDLC 文件放在视图中并将其直接渲染为 PDF。对此非常满意,因为我从来不需要在 MVC 应用程序中接触路由,也不需要添加任何带有控件或类似内容的 ASP.NET 页面。非常整洁的解决方案。

于 2012-07-31T05:58:44.853 回答