MVC3 Razor VS2010 ReportViewer example

Can anyone point me to a working example of showing a report in mvc3 ie the full code, I have looked at some examples and they look like they are going to do the job but then just blow up, I know that a ASPX page is needed and there seems to be a good example at How can I use a reportviewer control in an mvc 3 razor view? when I created a clean app i get Microsoft JScript runtime error: 'Microsoft' is undefined I know it is probably something simple but its been a while since I used Rdlc files so somthing showing a simple report populated via an xml datasource would be great, Thanks in advance


This is a simple task. You can follow the following steps.

  1. Create a folder in your solution and give a name Reports.
  2. Add a ASP.Net web form and named it ReportView.aspx
  3. Create a Class ReportData and add it to the Reports folder. Add the following code to the Class.

    public class ReportData
        public ReportData()
            this.ReportParameters = new List<Parameter>();
            this.DataParameters = new List<Parameter>();
    public bool IsLocal { get; set; }
    public string ReportName { get; set; }
    public List<Parameter> ReportParameters { get; set; }
    public List<Parameter> DataParameters { get; set; }
    public class Parameter
    public string ParameterName { get; set; }
    public string Value { get; set; }
  4. Add another Class and named it ReportBasePage.cs. Add the following code in this Class.

    public class ReportBasePage : System.Web.UI.Page
        protected ReportData ReportDataObj { get; set; }
        protected override void OnInit(EventArgs e)
            if (HttpContext.Current != null)
                if (HttpContext.Current.Session["ReportData"] != null)
                    ReportDataObj = HttpContext.Current.Session["ReportData"] as ReportData;
            ReportDataObj = new ReportData();
        private void CaptureRouteData(HttpRequest request)
            var mode = (request.QueryString["rptmode"] + "").Trim();
            ReportDataObj.IsLocal = mode == "local" ? true : false;
            ReportDataObj.ReportName = request.QueryString["reportname"] + "";
            string dquerystr = request.QueryString["parameters"] + "";
            if (!String.IsNullOrEmpty(dquerystr.Trim()))
                var param1 = dquerystr.Split(',');
                foreach (string pm in param1)
                    var rp = new Parameter();
                    var kd = pm.Split('=');
                    if (kd[0].Substring(0, 2) == "rp")
                        rp.ParameterName = kd[0].Replace("rp", "");
                        if (kd.Length > 1) rp.Value = kd[1];
                    else if (kd[0].Substring(0, 2) == "dp")
                        rp.ParameterName = kd[0].Replace("dp", "");
                        if (kd.Length > 1) rp.Value = kd[1];
  5. Add ScriptManager to the ReportView.aspx page. Now Take a Report Viewer to the page. In report viewer set the property AsyncRendering="false". The code is given below.

        <rsweb:ReportViewer ID="ReportViewerRSFReports" runat="server" AsyncRendering="false"
            Width="1271px" Height="1000px" >
  6. Add two NameSpace in ReportView.aspx.cs

    using Microsoft.Reporting.WebForms;
    using System.IO;

Change the System.Web.UI.Page to ReportBasePage. Just replace your code using the following.

public partial class ReportView : ReportBasePage
        protected void Page_Load(object sender, EventArgs e)
            if (!IsPostBack)

        private void RenderReportModels(ReportData reportData)
            RASolarERPData dal = new RASolarERPData();
            List<ClosingInventoryValuation> objClosingInventory = new List<ClosingInventoryValuation>();

            // Reset report properties.
            ReportViewerRSFReports.Height = Unit.Parse("100%");
            ReportViewerRSFReports.Width = Unit.Parse("100%");
            ReportViewerRSFReports.CssClass = "table";

            // Clear out any previous datasources.

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

            // Validate report source.
            var rptPath = Server.MapPath(@"./Report/" + reportData.ReportName +".rdlc");

            //@"E:\RSFERP_SourceCode\RASolarERP\RASolarERP\Reports\Report\" + reportData.ReportName + ".rdlc";

            if (!File.Exists(rptPath))

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

            // Set report parameters.
            var rpPms = ReportViewerRSFReports.LocalReport.GetParameters();
            foreach (var rpm in rpPms)
                var p = reportData.ReportParameters.SingleOrDefault(o => o.ParameterName.ToLower() == rpm.Name.ToLower());
                if (p != null)
                    ReportParameter rp = new ReportParameter(rpm.Name, p.Value);

            //Set data paramater for report SP execution
            objClosingInventory = dal.ClosingInventoryReport(this.ReportDataObj.DataParameters[0].Value);

            // Load the dataSource.
            var dsmems = ReportViewerRSFReports.LocalReport.GetDataSourceNames();
            ReportViewerRSFReports.LocalReport.DataSources.Add(new ReportDataSource(dsmems[0], objClosingInventory));

            // Refresh the ReportViewer.
  1. Add a Folder to the Reports Folder and named it Report. Now add a RDLC report to the Reports/Report folder and named it ClosingInventory.rdlc.

  2. Now add a Controller and Named it ReportController. In t o the controller add the following action method.

        public ActionResult ReportViewer()
            ViewData["reportUrl"] = "../Reports/View/local/ClosingInventory/";
            return View();
  3. Add a view page click on the ReportViewer Controller. Named the view page ReportViewer.cshtml. Add the following code to the view page.

        @using (Html.BeginForm("Login"))
              @Html.DropDownList("ddlYearMonthFormat", new SelectList(ViewBag.YearMonthFormat, "YearMonthValue", "YearMonthName"), new { @class = "DropDown" })
     Stock In Transit: @Html.TextBox("txtStockInTransit", "", new { @class = "LogInTextBox" })
    <input type="submit" onclick="return ReportValidationCheck();" name="ShowReport"
                        value="Show Report" />
  4. Add an Iframe. Set the property of the Iframe as follows

      frameborder="0"  width="1000"; height="1000"; style="overflow:hidden;" scrolling="no"
  5. Add Following JavaScript to the viewer.

        function ReportValidationCheck() {
        var url = $('#hdUrl').val();
        var yearmonth = $('#ddlYearMonthFormat').val();      
        var stockInTransit = $('#txtStockInTransit').val()
        if (stockInTransit == "") {
            stockInTransit = 0;
        if (yearmonth == "0") {
            alert("Please Select Month Correctly.");
        else {
            //url = url + "dpSpYearMonth=" + yearmonth + ",rpYearMonth=" + yearmonth + ",rpStockInTransit=" + stockInTransit;
            url = "../Reports/ReportView.aspx?rptmode=local&reportname=ClosingInventory&parameters=dpSpYearMonth=" + yearmonth + ",rpYearMonth=" + yearmonth + ",rpStockInTransit=" + stockInTransit;
            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;
  6. Web.config file add the following key to the appSettings section

    add key="UnobtrusiveJavaScriptEnabled" value="true"

In system.web handlers Section add the following key

add verb="*" path="Reserved.ReportViewerWebControl.axd" type = "Microsoft.Reporting.WebForms.HttpHandler, Microsoft.ReportViewer.WebForms, Version=, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
  1. Change your data source as your own. This solution is very simple and I think every one enjoy it.

Need Your Help

Send array from php to js with ajax and json

javascript php ajax json mysqli

I am trying to send an array from php (that I have taken from a mysql table to js). Although there a lot of examples out there I can't seem to make any of them work. The code that I have reached so...

Adding background image in MaterialViewPager

android android-viewpager

I'm using a library MaterialViewPager from github ( for my project. Instructions are given on the github page of library, but those aren't sufficient. I