SSRS Integration with Microsoft ASP.NET MVC 4 and Razor

C#

[UPDATE: I have dumped SSRS, Click Here for details.]

This is a massive excerpt from http://forums.asp.net/t/1963101.aspx?SSRS+Report+Viewer+in+MVC4

I will be rewriting this to meet my needs, and so that I don’t violate copyright. :)

There is a lot of code here that needs to be modified for this to follow a clean isolation, and adaptive layout.

View: ReportViewer.ascx

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="ReportViewerControl.ascx.cs" Inherits="ReportViewerControl" %>
<%@ Register TagPrefix="rsweb" Namespace="Microsoft.Reporting.WebForms" Assembly="Microsoft.ReportViewer.WebForms, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" %>

<form id="form1" runat="server">
<div style="Height:720px;Width:800px">
    <asp:ScriptManager ID="scriptManager" runat="server" ScriptMode="Release" EnablePartialRendering="false" />
    <rsweb:ReportViewer Width="100%" Height="100%" ID="reportViewer" ShowPrintButton="true" KeepSessionAlive="true" runat="server" AsyncRendering="false" ProcessingMode="Remote">
         <ServerReport />
    </rsweb:ReportViewer>
</div>
</form>

ReportViewer.ascx.cs


    public partial class ReportViewerControl : System.Web.Mvc.ViewUserControl
    {
        protected void Page_Init(object sender, EventArgs e)
        {
            // Required for report events to be handled properly.
            //reportViewer.ServerReport.ReportServerCredentials = new ReportServerCredentials();
            Context.Handler = Page;
            
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {                
                ReportingServicesReportViewModel model = (ReportingServicesReportViewModel)Model;
                reportViewer.ServerReport.ReportServerCredentials = model.ServerCredentials;
                ReportParameter[] RptParameters = model.parameters;
                
                reportViewer.ServerReport.ReportPath = model.ReportPath;
                reportViewer.ServerReport.ReportServerUrl = model.ReportServerURL;                

                if(RptParameters.Count() > 0)
                this.reportViewer.ServerReport.SetParameters(RptParameters);
                this.reportViewer.ServerReport.Refresh();
                
            }            
        }
    }   

ReportingServiceViewModel.cs

    public class ReportingServicesReportViewModel
    {
        #region Constructor
        public ReportingServicesReportViewModel(String reportPath,List<ReportParameter> Parameters)
        {
            ReportPath = reportPath;
            parameters = Parameters.ToArray();
        }
        public ReportingServicesReportViewModel()
        {           
        }
        #endregion Constructor

        #region Public Properties
        public ReportServerCredentials ServerCredentials { get { return new ReportServerCredentials(); } }
        public String ReportPath { get; set; }
        public Uri ReportServerURL { get { return new Uri(WebConfigurationManager.AppSettings["ReportServerUrl"]); } }
        public ReportParameter[] parameters { get; set; }
        private string UploadDirectory = HttpContext.Current.Server.MapPath("~/App_Data/UploadTemp/");
        private string TempDirectory = HttpContext.Current.Server.MapPath("~/tempFiles/");        
     }

you can use like this in controller:

HomeController.cs

public class HomeController
{

    public ActionResult ActionReport(int Id)
    {
      ReportingServicesReportViewModel model = new ReportingServicesReportViewModel(
      "ReportPath",
      new List<Microsoft.Reporting.WebForms.ReportParameter>()
      { 
        new Microsoft.Reporting.WebForms.ReportParameter("parameter1",Id.ToString(),false) 
                });
            return View("ViewReport", model);
        }
}

ViewReport.cshtml

@model ReportingInfrastructure.ReportingServicesReportViewModel
<div style="display: table; width: 100%;">
    @{
        @Html.Partial("ReportViewerControl", Model)
    }
</div>

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.