2

in my MVC3 razor application i used the following code for reporting

Contoller

ReportViewer rv = new Microsoft.Reporting.WebForms.ReportViewer();
    rv.ProcessingMode = ProcessingMode.Local;
    rv.LocalReport.ReportPath = Server.MapPath("~/Reports/TestReport.rdlc");
    rv.LocalReport.Refresh();

    byte[] streamBytes = null;
    string mimeType = "";
    string encoding = "";
    string filenameExtension = "";
    string[] streamids = null;
    Warning[] warnings = null;

    streamBytes = rv.LocalReport.Render("RDLC", null, out mimeType, out encoding, out filenameExtension, out streamids, out warnings);

    return File(streamBytes, mimeType, "TestReport.rdlc");

ASPX view

<div>
        <script runat="server">
            private void Page_Load(object sender, System.EventArgs e)
            {
                ReportViewer1.LocalReport.ReportPath = Server.MapPath("~/Reports/TestReport.rdlc");
                ReportViewer1.LocalReport.Refresh();
            }
        </script>
        <form id="Form1" runat="server" method="get" action="/Pag1/File">
        <asp:ScriptManager ID="ScriptManager1" runat="server">          
        </asp:ScriptManager>
        <rsweb:reportviewer id="ReportViewer1" runat="server" height="500" width="500" AsyncRendering="false"></rsweb:reportviewer>
        </form>        
    </div>

Here i got the result as PDF need to open that with pdfviewer. I just want to display the report in viewr . I am new to MVC3. If any body knows please share

Reference for the above code is here

Community
  • 1
  • 1
Nithesh Narayanan
  • 11,481
  • 34
  • 98
  • 138

2 Answers2

1

Here's an example of an ActionMethod I use in one of my sites to generate a report:

    public ActionResult WeeklyAisleReport(DateTime start, DateTime end)
    {
        var range = new DateRange(start, end);
        var records = _repository.Select(range, "");
        var formattedRecords = AisleProductivityRecord.Generate(records).ToList();

        var localReport = new LocalReport
        {
            ReportPath =
                Server.MapPath("~/Content/Reports/PTLWeeklyProductivity.rdlc")
        };


        var pickRecords = new ReportDataSource("PickRecords",formattedRecords);

        localReport.DataSources.Add(pickRecords);


        const string ReportType = "PDF";
        string mimeType;
        string encoding;
        string fileNameExtension;


        Warning[] warnings;
        string[] streams;

        //Render the report
        byte[] renderedBytes = localReport.Render(
            ReportType,
            null, //deviceInfo,
            out mimeType,
            out encoding,
            out fileNameExtension,
            out streams,
            out warnings);
        Response.AddHeader("content-disposition",
                           "attachment; filename=WeeklyAisleReport-" + start.ToString("yyyy_MM_dd") + "." +
                           fileNameExtension);
        return File(renderedBytes, mimeType);
    }

Regarding the View, you can't use WebForms tags (, , etc..) in an MVC Application. You'll need to create a form that posts to the ActionMethod that generates the PDF.

Your razor view file should look something like this (using my method as an example):

   @using (Html.BeginForm("WeeklyAisleReport", "PTL"))
   {
       @Html.TextBox("start")
       @Html.TextBox("end")
       <input type="submit" value="View Report"/>
   }
justinb138
  • 411
  • 2
  • 6
1

Add a Report Viewer and Script Manager to yous ASPX page from the Toolbox.

SizeToReportContent = "true"

Don't forget to add above attribute to your Report Viewer

Then your page looks like below

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ReportView.aspx.cs" Inherits="ERP.Reports.ReportView" %>

<%@ Register assembly="Microsoft.ReportViewer.WebForms, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" namespace="Microsoft.Reporting.WebForms" tagprefix="rsweb" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
       <rsweb:ReportViewer ID="ReportViewer1" runat="server" AsyncRendering="true" ShowPrintButton="true" Height="649px" Width="1105px"  SizeToReportContent = "true">

    </rsweb:ReportViewer>

    </div>
        <script>

        </script>
    </form>

</body>
</html>

Add this Code in your ASPX Code Page

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            RenderReportModels();
        }
    }
    private void RenderReportModels()
    {

        List<Company> comp = new List<Company>();
        CompanyBAL bal = new CompanyBAL();
        comp = bal.SampleData();//Load Data

        // Clear out any previous data sources.
        this.ReportViewer1.LocalReport.DataSources.Clear();

        // Set report mode for local processing.
        ReportViewer1.ProcessingMode = ProcessingMode.Local;

        // Validate report source.
        var rptPath = Server.MapPath(@"./rdlc/MyReport.rdlc");

        //@"E:\ERP\Reports\rdlc\MyReport.rdlc";

        if (!File.Exists(rptPath))
            return;

        // Set report path.
        this.ReportViewer1.LocalReport.ReportPath = rptPath;



        // Load the dataSource.
        var ds1 = ReportViewer1.LocalReport.GetDataSourceNames();
        ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource(ds1[0], comp));


        // Refresh the ReportViewer.
        ReportViewer1.LocalReport.Refresh();
    }

Add a Controller and corresponding View (cshtml) add below code to the view page

<div>
<input type="submit" onclick="return ReportValidationCheck();" name="ShowReport"
       value="Show Report" />
</div>
<iframe id="ifrmReportViewer" frameborder="0" width="1000" height="800" style="overflow:hidden;" scrolling="no"></iframe>

<script>
   function ReportValidationCheck() {
        var url = "../Reports/ReportView.aspx";  //your ASPX page path
        var myframe = document.getElementById("ifrmReportViewer");
        if (myframe !== null) {
            if (myframe.src) {
                myframe.src = url;
            }
            else if (myframe.contentWindow !== null && myframe.contentWindow.location !== null) {
                myframe.contentWindow.location = url;
            }
            else { myframe.setAttribute('src', url); }
        }
    return false;
  }
 </script>
rgb
  • 143
  • 3
  • 15