Create ReportViewer in Asp.Net MVC

1

How to create ReportViewer in Asp.Net Mvc 4 ? The Report is done, I just need to create the ReportViewer with the options to export pdf, excel and word.

Example:

 <rsweb:ReportViewer ID="ReportViewer1" runat="server" Font-Names="Verdana" Font-Size="8pt" Height="100%" WaitMessageFont-Names="Verdana" WaitMessageFont-Size="14pt" Width="100%">
            <LocalReport ReportEmbeddedResource="NFSWeb.ListagemRPS.rdlc">
                <DataSources>
                    <rsweb:ReportDataSource DataSourceId="ObjectDataSource1" Name="DsRPS" />
                </DataSources>
            </LocalReport>
        </rsweb:ReportViewer>

How can I make it the same as webforms in mvc?

    
asked by anonymous 09.09.2015 / 19:40

2 answers

3

You need to create a view like ReportView.ascx (that's right, webform), and then you need to create a report view model, please take a look.

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>

no code behind: ReportViewer.ascx.cs

namespace 
{
    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();

            }            
        }
    }   
}

You create another file to handle requests to the report server: 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/");        
     }

Configure credentials: ReportServerCredentials.cs

[Serializable]
    public sealed class ReportServerCredentials : IReportServerConnection2//IReportServerCredentials
    {

        #region Private Properties
        private string _username;
        private string _password;
        private string _domain;
        #endregion Private Properties

        #region Public Properties
        public System.Security.Principal.WindowsIdentity ImpersonationUser
        {
            get { return null; }
        }

        public System.Net.ICredentials NetworkCredentials
        {
            get { return new NetworkCredential(_username, _password, _domain); }
        }
        #endregion Public Properties

        #region Constructor
        public ReportServerCredentials(string userName, string password, string domain)
        {
            _username = userName;
            _password = password;
            _domain = domain;
        }
        public ReportServerCredentials()
        {
            var appSetting = WebConfigurationManager.AppSettings;
            _username = appSetting["ReportServerUser"];
            _password = appSetting["ReportServerPassword"];
            _domain = appSetting["ReportServerDomain"];
        }
        #endregion Constructor

        #region Public Method
        public bool GetFormsCredentials(out System.Net.Cookie authCookie, out string userName, out string password, out string authority)
        {
            authCookie = null;
            userName = null;
            password = null;
            authority = null;
            return false;
        }
        #endregion Public Method


        public IEnumerable<Cookie> Cookies { get { return null; } }


        public IEnumerable<string> Headers { get { return null; } }

        public Uri ReportServerUrl { get { return new Uri(WebConfigurationManager.AppSettings["ReportServerUrl"]); } }

        public int Timeout { get { return 60000; } }
    }

You can use this controller to call a new view that you will create: 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);
        }
}

Here is the view that this controller should point to: ViewReport.cshtml

@model ReportingInfrastructure.ReportingServicesReportViewModel
<div style="display: table; width: 100%;">

    @{

        @Html.Partial("ReportViewerControl", Model)

    }
</div>

Now, just call the controller and it should work.

    
12.07.2016 / 21:37
2

I'm assuming that you've already installed the Nuviet MvcReportViewer package in your project.

The steps to setup and use are here .

Examples for Controller configuration are here .

Their examples for Views are here .

    
09.09.2015 / 22:08