visual-studio-2008asp.net-mvc-2reportviewer

bind report to reportviewer in web mvc2


I have asp.net MVC2 application. I am using VS2008 and want to hook up generated report from my controller to reportviewer.

any ideas?

so far i have this code "Controller"

//should pass data to report
 public ActionResult GenerateReport()
      {


        LocalReport report = new LocalReport();
        report.ReportPath = Server.MapPath("~/Reports/KingsCourt.rdlc");

        List<InvoiceRow> rows = new List<InvoiceRow>();

        rows.Add(new InvoiceRow { name = "Testing item", value = (decimal)25.85 });
        rows.Add(new InvoiceRow { name = "Testing item2", value = (decimal)5.15 });
        ReportDataSource source = new ReportDataSource("InvoiceRow", rows);
        report.DataSources.Add(source);

        ViewData["InvoiceRow"] = report;
        return View();
      }

and View page:

 <form id="form1" runat="server">

  <h2>GenerateReport</h2>
  <rsweb:ReportViewer ID="ReportViewer1" runat="server" Font-Names="Verdana" 
      Font-Size="8pt" Height="400px" Width="400px">
    <LocalReport ReportPath="Reports\KingsCourt.rdlc">
      <DataSources>
        <rsweb:ReportDataSource DataSourceId="ObjectDataSource1" Name="InvoiceRow" />
      </DataSources>
    </LocalReport>


  </rsweb:ReportViewer>    

  <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="Rows" 
      TypeName="Accounts.Classes.Invoices"></asp:ObjectDataSource>
 </form>

Solution

  • You can always use ASP.NET WebForms with MVC. I guess this is the only way to do it.
    I've prepared a sample for you here.

    You can create a folder where you're going to put your asp.net WebForm and the report (rdlc). I've put in the same folder the schema (xsd) and the data (xml) but, obviously, I guess you're going to use the database. I've mapped the route to the report(s) like this:

        //Custom route for reports
        routes.MapRoute(
         "ReportRoute",
         "Reports/{reportname}",                
         "~/Reports/{reportname}.aspx"
         );
    

    UPDATE:

    I have prepared some code for ASP.NET MVC3 (MvcReportViewerMVC3).
    It is pretty much the same with some minor changes on the webform: I've integrated the new ReportViewer (10) and I had to add the ScriptManager to the same page:

    <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
    

    I've changed the WebForm code as well cause it appears that page_load event is called loads of times:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
        ....
        }
    }
    

    Hope it helps.