本文介绍了如何使用RDLC的ReportViewer控件在ASP.Net MVC报告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是相当新的ASP.Net MVC。我有示出的MVC基于RDLC​​报告的要求。

I am fairly new to ASP.Net MVC. I have a requirement of showing an RDLC based report in MVC.

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

Basically my requirement as well as what I have done is :-

我有一个ReportController继承APIController,其中有一个返回数据集的方法。这个数据集被发送到RDLC文件。

I have a ReportController inheriting APIController, which has a method that returns a DataSet. This DataSet is being sent to the RDLC file.

对于这一点,我已经做以下,但不能使报告的工作。

For this, I have done the following, but could not make the report work.

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

I have created a model class named ReportParameter as follows:

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

我有以下控制器ReportViewController:

I have the following controller 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控制器是:

I have an API Controller ReportController whose object has been created in the above ReportViewerController to generate a DataSet and to populate the RDLC report. The API Controller is:

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给出:

I have two views GenerateReport.aspx and ViewReport.aspx. The GenerateReport.aspx is given below:

<table style="width: 40%;">
              <tr>
                  <td class="style1">
                      <h3>
                          <asp:Label ID="Label1" runat="server" Text="From Date"></asp:Label></h3>
                  </td>
                  <td>
                      <%[email protected](a=> a.DateFrom, new{id="startDate",style="width:250px;"}) %>
                      <%[email protected](a => a.DateFrom)%>
                  </td>
              </tr>
              <tr>
                  <td class="style1">
                      <h3>
                          <asp:Label ID="Label2" runat="server" Text="To Date"></asp:Label></h3>
                  </td>
                  <td>
                      <%[email protected](a => a.DateTo, new { id = "ToDate", style = "width: 250px;" })%>
                      <%[email protected](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给出:

The ViewReport.aspx is given below:

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

        </iframe></center>

我添加了一个Dataset.xsd,一个RDLC文件和ASPX页面添加RDLC文件。

I have added a Dataset.xsd, an rdlc file and an aspx page to add the rdlc file.

但我不能使它工作。如何显示该报告,或者我怎么填充,我从控制器到报告接收数据集?

But I cannot make it work. How do I display the report, or how do I populate the dataset that I receive from the Controller to the report ?

推荐答案

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

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>

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

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

这篇关于如何使用RDLC的ReportViewer控件在ASP.Net MVC报告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 22:48