Is there a way to put a SQL Server Reporting Services report viewer control on an ASP.net MVC view? If not...what is the best way to accomplish this?
Under Installed | Templates, select Visual C# | ASP.NET Core | C1 ReportViewer View Page (ASP.NET Core) to open the C1 MVC ReportViewer dialog. In the C1 MVC ReportViewer dialog, select Report in SSRS server option. In the SSRS server field, enter a SSRS server URL to access the reports.
No, not in a MVC view. But you can have a web forms pages which have server controls in them mixed in with your MVC site.
Hmm, just googled "mix asp.net mvc and web forms" to find some examples, and google questioned whether I'm human or not :)
Anyway, here's a link - http://www.packtpub.com/article/mixing-asp.net-webforms-and-asp.net-mvc - there's a few out there. I've also done this in a MVC site for the same reason - the report control.
No, the ReportViewer control won't work if you place it in an MVC view, since it requires ViewState. You'll have to create an old-school web form and put the ReportViewer there instead.
A solution I used in a project I worked on was to create a custom route handler, so I could still make use of URL routing. The route handler would take parameters like the report name from the RouteData collection, create an instance of my web form, and pass the parameters to it via public properties. The web form would read these in Page_Load and configure the ReportViewer control.
// Configure a route in Global.asax.cs that is handled by a ReportRouteHandler routes.Add("ReportRoute", new Route("Reports/{reportName}", new ReportRouteHandler()); public class ReportRouteHandler : IRouteHandler { public IHttpHandler GetHttpHandler(RequestContext requestContext) { var reportName = requestContext.RouteData.Values["reportName"] as string; var webform = BuildManager .CreateInstanceFromVirtualPath("~/Path/To/ReportViewerWebForm.aspx", typeof(Page)) as ReportViewerWebForm; webform.ReportToShow = reportName; return webform; } }
This code is just a starting point if you decide to use this approach, of course. The one I created also did some user authentication and parameter validation before returning.
Update: Looks like if you're using ASP.NET 4.0, most of this can be done automatically!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With