在渲染某些字段时,RDLC与MVC的集成新视图#Error

时间:2012-07-23 02:34:34

标签: asp.net-mvc asp.net-mvc-3 rdlc

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

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

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

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

唯一使2 #Error列与渲染的列不同的是它们是模型的一个级别。即4列:

第一个字段SiteDescription呈现正常 第二个字段SiteOperator将值呈现为每行的#Error 第3个字段SiteStatus将值呈现为每行的#Error 第4个字段CapacityMW渲染确定

注意:SiteOperator是Site.SiteOperator.Operator。类似于第3场。第一个和第四个是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();
    }

3 个答案:

答案 0 :(得分:0)

在RDLC中接收“#Error”响应时要注意的第一件事是Visual Studio中的“输出/调试”选项卡。对于报表引擎在呈现报表时收到的每个错误,都应该有一条特定的错误消息。虽然并非总是世界上最直观的信息,但它们至少应该指向正确的方向。

答案 1 :(得分:0)

我在MVC中使用RDLC,其模型源自Entity Framework生成的C#类(使用POCO对象模板)。

现在,我的类是一个相对“扁平”的类,只有属性(nullables,numerics,strings,DateTimes等),没有其他对象的集合,所以可能会有所不同。

我在没有使用向导的情况下创建了我的RDLC,但是发现要在MVC中使用设计器,我必须在项目中添加.aspx以便设计人员工作。我也只是在mock类中有公共静态方法,它返回我用于报告数据源向导等消费类型的IEnumerables。

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

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

我也返回PDF字节,因此您的代码看起来非常接近我使用的内容(我使用数据源和参数,再加上子报告)。

您没有描述您的类结构是什么样的,但我认为它存在层次结构问题,因此您可能需要将其展平为报表编写者喜欢的内容。

答案 2 :(得分:0)

我设法让报告运作良好。我切换到使用存储过程并从控制器调用它。我将RDLC文件放在视图中并直接渲染为PDF。真的很高兴,因为我从来没有在MVC应用程序中触摸路由,我从来没有添加任何带有控件的ASP.NET页面或类似的东西。非常干净和干净的解决方案。