Create Reportviewer in Asp.Net MVC

Asked

Viewed 1,921 times

1

How to create Reportviewer in Asp.Net Mvc 4 ? The Report is done, just need to create 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 do this like the webforms in mvc ?

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 at.

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>

in 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 the 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 out: ViewReport.cshtml

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

    @{

        @Html.Partial("ReportViewerControl", Model)

    }
</div>

Okay, now just make a call to the controller and should work.

2

  • Yes, I installed the package. I’ll see, thanks.

  • dude, I just need to create the same report in webforms in my view, but in mvc. How can I do this in mvc ? I rephrased the question.

  • @Andreeh You came to read the examples?

  • I arrived but found it a bit complex, and I tried here and the view is not recognizing the reference of Mvcreportviewer.

  • It means that configuration is missing. Open another question and explain this difficulty.

  • All right, I’ll open it. Thanks.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.