【问题标题】:How can I use RDLC reports with the ReportViewer Control in ASP.Net MVC?如何在 ASP.Net MVC 中将 RDLC 报告与 ReportViewer 控件一起使用?
【发布时间】:2015-01-01 09:23:27
【问题描述】:

我对 ASP.Net MVC 还很陌生。我需要在 MVC 中显示基于 RDLC 的报告。

基本上我的要求以及我所做的是:-

我有一个继承 APIController 的 ReportController,它有一个返回 DataSet 的方法。此数据集正在发送到 RDLC 文件。

为此,我执行了以下操作,但无法使报告正常工作。

我创建了一个名为 ReportParameter 的模型类,如下所示:

public class ReportParameter 
{
    public DateTime DateFrom { get; set; }
    public DateTime DateTo { get; set; }
}

我有以下控制器 ReportViewController :

public class ReportViewController : Controller
{
    static readonly ReportController ctrl = new ReportController();

    public ActionResult GenerateReport()
    {
        return View();
    }

    [HttpPost]
    public ActionResult GenerateReport(ReportParameterSalesOrder param)
    {
        if (ModelState.IsValid)
        {
            Helpers.DataLayer dl = new Helpers.DataLayer();
            if (param.DateFrom != null)
            {
                DateTime DateFrom = Convert.ToDateTime(param.DateFrom);
                DateTime DateTo = Convert.ToDateTime(param.DateTo);

                string fdate = DateFrom.ToString("yyyy/MM/dd");
                string tdate = DateTo.ToString("yyyy/MM/dd");

                Session["ReportSales"] = ctrl.ReportSales(param);
            }

            return Redirect(Url.Action("ViewReport", "ReportView"));
        }
        return View();
    }
    public ActionResult ViewReport()
    {
         return View();
    }

}

我有一个 API 控制器 ReportController,其对象已在上述 ReportViewerController 中创建,用于生成 DataSet 并填充 RDLC 报告。 API 控制器是:

public class ReportController : ApiController
{

    static readonly IReportRepository repository = new ReportRepository();

    [ActionName("ReportSales")]
    public DataSet ReportSales(ReportParameterSalesOrder paramSO)
    {
        DataSet item = repository.ReportSales(paramSO);
        if (item == null)
        {
            throw new HttpResponseException(HttpStatusCode.NotFound);
        }
        return item;
    }
}

我有两个视图 GenerateReport.aspx 和 ViewReport.aspx。 GenerateReport.aspx 如下:

<table style="width: 40%;">
              <tr>
                  <td class="style1">
                      <h3>
                          <asp:Label ID="Label1" runat="server" Text="From Date"></asp:Label></h3>
                  </td>
                  <td>
                      <%=@Html.EditorFor(a=> a.DateFrom, new{id="startDate",style="width:250px;"}) %>
                      <%=@Html.ValidationMessageFor(a => a.DateFrom)%>
                  </td>
              </tr>
              <tr>
                  <td class="style1">
                      <h3>
                          <asp:Label ID="Label2" runat="server" Text="To Date"></asp:Label></h3>
                  </td>
                  <td>
                      <%=@Html.EditorFor(a => a.DateTo, new { id = "ToDate", style = "width: 250px;" })%>
                      <%=@Html.ValidationMessageFor(a => a.DateTo)%>
                  </td>
              </tr>
              <tr>
                  <td class="style1">
                      &nbsp;
                  </td>
                  <td>
                      &nbsp;
                  </td>
              </tr>
              <tr>
                  <td class="style1">
                      &nbsp;
                  </td>
                  <td>
                      <input id="btnsearch" class="button" type="submit" value="Show" />
                  </td>
              </tr>
          </table>

ViewReport.aspx 如下:

 <center style="width: 974px">
      <iframe id="myReport" width="100%" height="450px" src="ReportViewer.aspx">

        </iframe></center>

我已经添加了一个Dataset.xsd、一个rdlc文件和一个aspx页面来添加rdlc文件。

但我不能让它工作。如何显示报告,或者如何将我从控制器收到的数据集填充到报告中?

【问题讨论】:

  • ViewReport.aspx 中使用的iframe 需要是一个带有代码的老式 Web 表单 - 例如在ViewReport.aspx.cs 后面的代码中设置DataSet。您可能会做的是通过 Session[] 变量从 MVC 控制器传递数据?
  • ViewReport.aspx 没有代码隐藏文件。能再解释一下吗?

标签: c# asp.net-mvc asp.net-mvc-4 reporting-services rdlc


【解决方案1】:

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

  • Microsoft ReportViewer 控件需要 ViewState 和 WebForms ScriptManager 才能正常工作,因此不适合在 MVC 视图中直接使用。
  • 但是,可以在 MVC 项目中运行 WebForms 页面 - 因为它们在同一个 AppDomain 中运行,Session 状态在 MVC 和 WebForms 之间共享。

详细说明
用于在 MVC 视图上的 iframe 中呈现 ReportViewer 控件的 ViewReport.aspx 页面需要是一个良好的老式 asp.Net Web 表单。

对于小型数据集,您可以在 MVC 控制器中获取报告数据,然后将其通过 Session 传递给 WebForm。

但是,对于较大的数据集,我建议您改为将参数传递给 Session 中的 WebForm(或者甚至通过 QueryString,如果它们不敏感),然后后面的 WebForm 代码将需要获取数据集并将其绑定到 ReportViewer。

在 MVC 端,MyController 参数帖子中:

    [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();
    }

一旦用户在视图MyController/GenerateReport 上输入 ReportParameters,您就可以显示 IFrame:

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

然后将 WebForms 页面 /OldSkoolAspx/ReportViewer.aspx 添加到您的 MVC 项目中。在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):

    <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 over here

请注意,同样的技术也适用于 Report Server 生成的报告(即使用带有 .RDL 报告的 ReportViewer)。但是请注意 RDLC 和 RDL 都可能是真实的SessionState hogs

【讨论】:

  • 谢谢@StuartLC,你让我开心。为了展示一份报告,我摸不着头脑。
  • 嗨,但问题是当我的报告加载其他功能时,即使我尝试将 asnycrender 设置为 true,所有功能只有在报告完全加载后才能工作,有什么线索吗?
  • 伙计,它帮了我很大的忙。 :)
猜你喜欢
  • 1970-01-01
  • 2012-01-24
  • 2015-12-30
  • 1970-01-01
  • 2017-03-08
  • 1970-01-01
  • 2016-04-16
  • 2021-05-17
  • 2010-12-20
相关资源
最近更新 更多