views:

63

answers:

1

Basically I want to know how to embed a report into MVC.Net 2.

+3  A: 

I did the question, because there's not enough information on the web, or the information is not that complete so you can start working.

The first thing you have to know is that the report viewer is a webcontrol so you can't use it on MVC, so first thing you have to do is create a web form so you can add the report viewer. In the example I've done I'm using Visual Studio 2010.

The webform looks like this:

<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head runat="server">
    <title>Report Viewer</title>
</head>
<body>
    <div style="width: auto;">
        <form id="form1" runat="server" style="width: 100%; height: 100%;">
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <rsweb:ReportViewer ID="rptViewer" runat="server" Width="100%" Height="100%" AsyncRendering="False"
            SizeToReportContent="True">
        </rsweb:ReportViewer>
        </form>
    </div>
</body>
</html>

The code behind of the web form:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        var reportServer = ConfigurationManager.AppSettings["ReportServer"].ToString();
        var reportPath = ConfigurationManager.AppSettings["ReportPath"].ToString();

        rptViewer.ServerReport.ReportServerUrl = new Uri(reportServer);
        rptViewer.ShowToolBar = false;
        rptViewer.ServerReport.ReportPath = reportPath + Request.QueryString["ReportName"];
        //var parameters = new List<ReportParameter>();
        //parameters.Add(param1));                
        //rptViewer.ServerReport.SetParameters(parameters);
        rptViewer.ProcessingMode = ProcessingMode.Remote;
        rptViewer.ServerReport.Refresh();
    }
}

Now we need to use the MVC. We have two options one, open a new window with a javascript pop up or use an iframe.

I will do both so you can have a best idea on the View:

<iframe id="Frame1" src="<%= Session["Url"] %>" width="230" height="230" frameborder="0"></iframe> **1
 function OpenReports(name) {
            var width = (screen.availWidth - 700).toString();
            var height = (screen.availHeight - 100).toString();
            window.open('/Reporting/Reports.aspx?ReportName=' + name,
                 'mywindow', 'width=' + width + ',height=' + height + ',toolbar=no,location=no,directories=yes,status=no,' +
                'menubar=no,scrollbars=yes,copyhistory=yes,resizable=yes' + ',screenX=0,screenY=0,left=0,top=0');

        } **2

**1 SessionURL is a Session Variable with the path and report we want to show. Also this is the first way to do embed the Report using an iframe

**2 /Reporting/Reports.aspx is the path of the webform we just did eaelier. This is the second way, opening a new window.

In the Controller:

 public ActionResult ViewName()
 {
    Session["Url"] = "/Reporting/Reports.aspx?ReportName=Report44";
    return View();
 }**1

**1 /Reporting/Reports.aspx is the path of the webform we just did eaelier.

Hope all this tutorial helps somebody :)

Sergio
It did help somebody - me. Thanks very much.
Peanut

related questions