且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用RDLC的ReportViewer控件在ASP.Net MVC报告?

更新时间:2023-02-16 12:30:06

背景结果
(我知道你知道这一点,但对于未来的读者; - )

Background
(I know you know this, but for future readers ;-)


  • 微软的ReportViewer控制需要的ViewState和WebForms的ScriptManagers正常工作,因此不适合在MVC视图直接使用。

  • 不过,可以在一个MVC项目运行WebForms的页面 - 因为他们在同一个AppDomain中运行,会话状态MVC和WebForms的共享李。 >
  • The Microsoft ReportViewer Control requires ViewState and WebForms ScriptManagers to work correctly, and as such isn't suited for direct use in MVC Views.
  • It is however possible to run a WebForms page in an MVC Project - as they run in the same AppDomain, Session state is shared between MVC and WebForms.

详情结果
用于呈现在 IFRAME 在MVC视图ReportViewer控件的 ViewReport.aspx 页将需要一个良好的老式asp.Net Web表单。

In Detail
The ViewReport.aspx page used to render the ReportViewer control in the iframe on the MVC View will need to be a good old fashioned asp.Net web form.

有关小数据集,您可以获取在MVC控制器的报告数据,然后通过这个在会话对面的web窗体。

For small data sets, you can fetch the report data in the MVC Controller and then pass this in Session across to the WebForm.

不过,对于更大的数据集,我建议你,而不是整个传递参数给会话中的WebForm(甚至通过查询字符串,如果他们不敏感),然后将Web窗体code背后需要获取的数据集,并将其绑定到的ReportViewer。

However, for larger data sets, I would recommend that you instead pass the Parameters across to the WebForm in Session (or even via the QueryString, if they aren't sensitive), and then the WebForm code behind would need to fetch the data set and bind it to the ReportViewer.

在MVC的侧面,在 myController的参数后:

On the MVC Side, in the MyController parameter post:

    [HttpPost]
    public ActionResult GenerateReport(string param1, int param2)
    {
        // Obviously you apply the parameters as predicates and hit the real database
        Session["ReportData"] = FakeDatabaseData;
        ViewBag.ShowIFrame = true;
        return View();
    }

您可以显示的IFrame一旦ReportParameters已经键入的用户,打开查看, myController的/ GenerateReport

You can show the IFrame once the ReportParameters have been typed in by the user, on the View, MyController/GenerateReport:

<iframe src='<%= Url.Content("~/OldSkoolAspx/ReportViewer.aspx") %>' width="100%" height="450px"></iframe>

然后,WebForms的页面 /OldSkoolAspx/ReportViewer.aspx 添加到您的MVC项目。在后面的code ReportViewer.aspx.cs

Then add a WebForms page /OldSkoolAspx/ReportViewer.aspx to your MVC project. In the code behind ReportViewer.aspx.cs:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            var reportDataSource = new ReportDataSource
            {
                // Must match the DataSource in the RDLC
                Name = "SomeReportDataSet",
                Value = Session["ReportData"]
            };
            ReportViewer1.LocalReport.DataSources.Add(reportDataSource);
            ReportViewer1.DataBind();
        }
    }

而在WebForms的 ReportViewer.aspx 前端,添加控件(推荐使用工具箱中,因此所有必要的引用被添加到web.config):

And in the WebForms ReportViewer.aspx front end, add the control (recommend use the toolbox, so all the requisite references get added to the web.config):

    <rsweb:ReportViewer ID="ReportViewer1" runat="server" Font-Names="Verdana" Font-Size="8pt" WaitMessageFont-Names="Verdana" WaitMessageFont-Size="14pt" Width="476px">
        <LocalReport ReportPath="MyReport.rdlc">
        </LocalReport>
    </rsweb:ReportViewer>
    <asp:ScriptManager runat="server" ID="SillyPrerequisite"></asp:ScriptManager>

还有很多在这里的运动部件,所以我上传了一个示范项目,在这里 GitHub上

There's a lot of moving parts here, so I've uploaded a demo project to GitHub over here

请注意,同样的技术也将为报表服务器运行生成的报告(即,使用的ReportViewer用的.rdl报告)。不过千万要小心,无论RDLC RDL和可以真正的SessionState$c$c>猪

Note that the same technique will also work for Report Server generated reports (i.e. using ReportViewer with .RDL reports). Do however be wary that both RDLC and RDL can be real SessionState hogs