I've seen many people saying to use an iframe or a new page to display a ReportViewer control. Is there a way to display the control inline with the rest of my page without using an iframe?
-
3possible duplicate of [How can I use a reportviewer control in an asp.net mvc 3 razor view?](http://stackoverflow.com/questions/6144513/how-can-i-use-a-reportviewer-control-in-an-asp-net-mvc-3-razor-view) – jrummell Mar 04 '13 at 18:32
-
Wow - I definitely did not find that piece while searching myself. The **ViewUserControl1.ascx** part of the accepted answer is exactly it. – zimdanen Mar 04 '13 at 18:36
-
Another related question [**Viewing SSRS Reports in an ASP.NET MVC Site**](https://stackoverflow.com/a/27231670/1366033) – KyleMit Jun 14 '18 at 04:56
1 Answers
You can use .ascx
user controls as partial views with Razor if they inherit from System.Web.Mvc.ViewUserControl
.
In this instance, you can create an ASCX
that contains your ReportViewer
control and the requisite ScriptManager
in your View\Controller
folder:
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="ReportViewerControl.ascx.cs" Inherits="MyApp.Views.Reports.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>
<asp:ScriptManager ID="scriptManager" runat="server" EnablePartialRendering="false" />
<rsweb:ReportViewer Width="100%" Height="100%" ID="reportViewer" runat="server" AsyncRendering="false" ProcessingMode="Remote">
<ServerReport />
</rsweb:ReportViewer>
</div>
</form>
In the code-behind, make sure to include the following in the Page_Init
; otherwise, you won't be able to use any options in the report view:
protected void Page_Init(object sender, EventArgs e)
{
// Required for report events to be handled properly.
Context.Handler = Page;
}
You also want to make sure that your control inherits from System.Web.Mvc.ViewUserControl
:
public partial class ReportViewerControl : ViewUserControl
To use this control, you would do something like this in your Razor page:
@Html.Partial("ReportViewerControl", Model)
You can then setup your ReportViewer in the Page_Load
of the control as you normally would. You will have access to an object
named Model
, which you can cast to the type of the model that you send in and then use:
ReportViewParameters model = (ReportViewParameters)Model;

- 5,508
- 7
- 44
- 89
-
1It's looking great but I want to have my users get a report by some parameters. After clicking the button the report viewer button "View Report" I'm faced with the following exception: System.Web.Mvc.HttpAntiForgeryException: The required anti-forgery form field "__RequestVerificationToken" is not present. Any idea how to resolve this problem? – OrangeCactus Aug 28 '14 at 12:28
-
The report viewer component is doing some AJAX callbacks.... I'm using Orchard and its securing the controller actions with Antiforgery attributes. You can turn that off in Orchard, but you then just have to set the Antiforgery attribute on specific controllers\actions that you want it to be on. – Mike D Sep 05 '14 at 15:01
-